← 返回 amazon 的题目列表Grid BFS: Minimum steps in a 2D matrix (simplified interview variant)
类型:online_judge
Coding: Minimum steps in a 2D grid (BFS)
You are given a 2D matrix grid where:
grid[r][c] = 0 means the cell is walkable
grid[r][c] = 1 means the cell is blocked
You are also given a start cell S = (sr, sc) and a target cell T = (tr, tc).
In one step, you may move to one of the 4-directionally adjacent cells (up/down/left/right). You may not leave the grid or step onto blocked cells.
Return the minimum number of steps required to reach T from S. If it is impossible, return -1.
Input (stdin)
Line 1: two integers m n
Next m lines: n integers (0/1) describing grid
Next line: sr sc (start)
Next line: tr tc (target)
Output (stdout)
One integer: minimum steps or -1
Constraints
1 <= m, n <= 200
Test cases
Example
Input
3 3
0 0 0
1 1 0
0 0 0
0 0
2 2
Output
4