ROBOTGRI - Robots on a grid

You have recently made a grid traversing robot that can find its way from the top left corner of a grid to the bottom right corner. However, you had forgotten all your AI programming skills, so you only programmed your robot to go rightwards and downwards (that's after all where the goal is). You have placed your robot on a grid with some obstacles, and you sit and observe. However, after a while you get tired of observing it getting stuck, and ask yourself "How many paths are there from the start position to the goal position?", and "If there are none, could the robot have made it to the goal if it could walk upwards and leftwards?" So you decide to write a program that, given a grid of size n x n with some obstacles marked on it where the robot cannot walk, counts the dierent ways the robot could go from the top left corner s to the bottom right t, and if none, tests if it were possible if it could walk up and left as well. However, your program does not handle very large numbers, so the answer should be given modulo 231 - 1.

Input

On the first line is one integer, 1 < n <= 1000. Then follows n lines, each with n characters, where each character is one of '.' and '#', where '.' is to be interpreted as a walkable tile and '#' as a non-walkable tile. There will never be a wall at s, and there will never be a wall at t.

Output

Output one line with the number of dierent paths starting in s and ending in t (modulo 231 - 1) or THE GAME IS A LIE if you cannot go from s to t going only rightwards and downwards but you can if you are allowed to go left and up as well, or INCONCEIVABLE if there simply is no path from s to t.

Example

Input:
.....
#..#.
#..#.
...#.
.....

Output:
6

Added by:Krzysztof Lewko
Date:2011-10-05
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Nordic programming contest

hide comments
2014-07-09 00:26:25 Mitch Schwartz
@Andres Felipe Ruiz Cardozo: Judging does not halt on first failure, the way SPOJ is currently set up. (You can see for yourself by submitting infinite loop code.)
2014-07-08 22:49:25 Andres Felipe Ruiz Cardozo
This problem is frustrating!! TLE on test 18 and don't know what else to tinker with.

EDIT: Actually I am an idiot. It's entirely my fault!

Last edit: 2014-07-09 00:35:16
2014-04-20 05:19:30 Black Hole
finally AC. same code but one is TLE, the other is AC :))
2014-02-11 17:43:03 newbie
finally AC
2014-01-28 17:50:09 Titas Skrebė
be very careful with that goddamn modulo.
2013-09-22 21:39:16 Ankur Ahuja
What's the case with the 18th test case ? WHY TLE ? Any suggestions ? Hints ?
2013-07-11 18:04:45 BLANKRK
huhh.......finaly AC....so many runtime errrorss.....
2013-05-29 08:33:18 Mayank Thakur
WA in 18th case even after taking care of (2^31-1)%(2^31-1) :/
when corrected the mistake its giving runtime on 15th...sadness

Last edit: 2013-05-29 13:02:27
2013-03-30 14:57:31 vishal goel
check modulus

Last edit: 2014-01-17 11:42:26
2013-03-26 18:18:12 Janani
Classic problem! Take care of overflows, and keep in mind what you will get when you do (1<<31-1)%(1<<31-1).

Last edit: 2013-03-26 18:18:33
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.