Given an n x n
binary matrix grid
, return the length of the shortest clear path in the matrix. If there is no clear path, return -1
.
A clear path in a binary matrix is a path from the top-left cell (i.e., (0, 0)
) to the bottom-right cell (i.e., (n - 1, n - 1)
) such that:
0
.The length of a clear path is the number of visited cells of this path.
Example 1:
Input: grid = [[0,1],[1,0]] Output: 2
Example 2:
Input: grid = [[0,0,0],[1,1,0],[1,1,0]] Output: 4
Example 3:
Input: grid = [[1,0,0],[1,1,0],[1,1,0]] Output: -1
Constraints:
n == grid.length
n == grid[i].length
1 <= n <= 100
grid[i][j] is 0 or 1
This problem asks for the shortest path from the top-left to the bottom-right corner of a binary matrix, where only cells with value 0 can be traversed, and movement is allowed in all 8 directions.
The most efficient approach is using Breadth-First Search (BFS). BFS guarantees finding the shortest path in an unweighted graph (which our matrix represents).
Algorithm:
Initialization:
grid[0][0]
is 1 (blocked). If so, return -1 (no path).q
and add the starting cell coordinates (0, 0)
to it.grid[0][0]
as visited (e.g., by setting its value to 1).ans
(path length) to 1.BFS Iteration:
q
is not empty:
len(q)
times in the loop.(i, j)
dequeued:
(i, j)
is the bottom-right cell (i == n - 1 && j == n - 1
), return ans
(shortest path found).(i, j)
:
0 <= x < n
and 0 <= y < n
).grid[x][y] == 0
).grid[x][y] = 1
).q
.ans
(path length increases by one for each level).No Path:
Time Complexity Analysis:
Space Complexity Analysis:
q
. In the worst-case, the queue can hold all cells in the matrix.Code Examples (with explanations inline):
The code examples in Python, Java, C++, Go, TypeScript, and Rust provided in the original response all implement this BFS algorithm effectively. They differ slightly in syntax and data structure usage (e.g., deque
vs. queue
, array vs. list), but the core logic remains the same. The in-line comments in the code provide further explanations.
The key improvement in these solutions compared to a simple recursive DFS is that BFS systematically explores the matrix level by level, guaranteeing that the shortest path is found first. DFS, on the other hand, can get trapped in exploring longer paths before finding the optimal solution.