Lv.30-1-bfsgridshortest-path
Escape Breaking the Fewest Walls
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
In grid, 0 is open and 1 is a wall that can be broken. Moving in four directions from (0, 0) to the bottom-right cell,
return the minimum number of walls to break. Both corners are open.
Examples
| grid | result |
|---|---|
[[0, 1, 1], [1, 1, 0], [1, 1, 0]] | 2 |
[[0, 0, 0], [1, 1, 0], [0, 0, 0]] | 0 |
[[0]] | 0 |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(grid: int[][]) → int
Constraints
- ;
grid[i][j]is0or1; both corners are0
Expected complexity
- Time
- O(HW)
- Space
- O(HW)
Examples
| grid | Expected |
|---|---|
[[0,1,1],[1,1,0],[1,1,0]] | 2 |
[[0,0,0],[1,1,0],[0,0,0]] | 0 |
[[0]] | 0 |
Hint
Hint
Shortest path with edge costs 0 (open cell) or 1 (wall): 0-1 BFS with a deque (push cost-0 moves to the front, cost-1 to the back), or Dijkstra with a heap. Array-scan Dijkstra is .
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1074 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 8 | verdict only |
| performance | 60 | 4 | verdict only |