Minimum obstacle removal
Standard · Write code · about 20 min
Each cell of grid is 0 (empty) or 1 (obstacle). You start at the top-left cell and want to reach the bottom-right cell, moving up, down, left or right. You may remove obstacles.
Return the fewest obstacles you must remove.
Example 1
Input:
[[[0,1,1],[1,1,0],[1,1,0]]]
Output:
2
Remove (0, 1) and (0, 2), then walk down the open right column.
Example 2
Input:
[[[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]]
Output:
0
A route around the obstacles exists.
Constraints
- 1 ≤ rows, cols ≤ 100
- grid[r][c] is 0 or 1
- grid[0][0] = grid[rows − 1][cols − 1] = 0
Learn the idea first: 0-1 BFS and multi-source BFS, a visual lesson.
More 0-1 BFS and multi-source BFS problems
- Front or back of the deque Warm-up
- Which structure for 0 and 1 weights? Warm-up
- Rotting oranges Standard
- Distance to the nearest 0 Standard