Space Scramble
Due to foreseen circumstances that were well within his control, P has found himself in quite a predicament - having to solve space-bending mazes.
The maze is presented as an n x n matrix M. P can enter the matrix from any corner of the maze, but the exit lies in the maze at a position [u][v]. At position [i][j], he can be teleported to a position M[i][j] away (e.g. position [i][j+M[i][j]]) within the maze (i.e. no wrap around). Determine if the maze is solvable.
Input
The first line contains an integer n, the size of the maze. The second line contains two integers u,v, the position of the exit. The following n lines contains a row in the matrix M.
Output
True if we can reach the exit from any of the four corners otherwise False.
Constraints
- 3≤n≤5000
- 1≤u,v≤n
- 0≤M[i][j]≤n
Example 1
3
2 2
1 3 0
1 0 1
0 2 1
True
Example 2
5
3 4
3 4 0 1 2
3 2 1 4 0
4 1 3 0 2
4 2 3 0 1
0 4 2 1 3
False
Comments0
No comments yet
Be the first to comment.
New comment
Log in to join the discussion.