← 返回 meta 的题目列表Shortest Path in Grid with Obstacles
类型:online_judge
Given an n x n grid where grid[i][j] == 0 represents no obstacle and grid[i][j] == 1 represents an obstacle, find the shortest path from top left to bottom right corner.
Input:
A n x n 2D integer array grid (0 <= grid[i][j] <= 1, 1 <= n <= 100)
Output:
The length of the shortest path from top left to bottom right. Return -1 if there is no such path.
You can move in all four cardinal directions (up, down, left, right) from the current cell.
Example:
Input:
3
0 0 1
0 1 0
0 0 0
Output:
4
In the example above, the shortest path is (0,0) -> (0,1) -> (1,1) -> (1,2) -> (2,2), with a length of 4.
Example
Input
3
0 0 1
0 1 0
0 0 0