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
2021-06-25 11:59:51
I was so excited ...that I made so many silly mistakes...n read problem carefully we don't need to count no of ways by going left n upwards...just down n right for dp n bfs ;)
2021-05-08 12:31:19
SPOJ NEVER FAILS TO IMPRESS
2020-04-20 15:35:48
My first AC in one go in SPOJ.
Some 2D DP and BFS

Last edit: 2020-04-20 15:36:24
2020-04-13 08:38:54
just google 2^31 -1 and copy paste if you are having issues. Also take care of the output format. i typed "THIS" instead of "THE" resulting in WA's. felt foolish.
2020-02-19 17:16:39
Multiple WA due to MOD.
ll MOD = ((ll)1<<31) - 1;
2019-12-30 19:06:44
AC in one go.
2019-11-14 11:10:24
For people getting WA on test case 18, check how your bfs works for the first row and first column, the invalid moves are causing WA.
2019-11-09 18:09:41
easy BFS problem, use queue no need for recursion
2019-11-09 07:24:47
i am using top -down dp here but still getting TLE .whyyyyy?
2019-05-22 07:29:26
You have to actually print "THE GAME IS A LIE " or " INCONCEIVABLE" (without quotes) ! Take care of modulo by typecasting 1<<31 to ((long long)1<<31).
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.