Space Scramble

View as PDF

Submit solution

Points: 100
Time limit: 2.0s
Memory limit: 977M

Problem type

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 \le n \le 5000
  • 1 \leq u, v \leq n
  • 0 \leq M[i][j]\leq n

Example 1

Input
3
2 2
1 3 0
1 0 1
0 2 1
Output
True

Example 2

Input
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
Output
False

Comments

There are no comments at the moment.