← 返回 amazon 的题目列表Path Exists in a Binary Maze
类型:online_judge
Problem: Path Exists in a Binary Maze
You are given an m x n binary maze grid:
1 means the cell is walkable;
0 means the cell is blocked.
You are also given a start position (sr, sc) and a target position (tr, tc). In one step, you may move one cell in one of the four directions: up, down, left, or right. You may only move to cells whose value is 1.
Determine whether there exists a path from the start cell to the target cell.
Print true if such a path exists; otherwise print false.
Input Format
m n
grid[0][0] grid[0][1] ... grid[0][n-1]
...
grid[m-1][0] grid[m-1][1] ... grid[m-1][n-1]
sr sc tr tc
Output Format
true
or
false
Constraints
1 <= m, n <= 1000
1 <= m * n <= 2 * 10^5
grid[i][j] is either 0 or 1
0 <= sr, tr < m
0 <= sc, tc < n
If the start or target cell is 0, it is considered unreachable.
Example
Input:
3 3
1 1 0
0 1 0
0 1 1
0 0 2 2
Output:
true
Example
Input
3 3
1 1 0
0 1 0
0 1 1
0 0 2 2
Output
true