This is a shortest path problem in an unweighted grid, so BFS is the optimal approach. 8-directional movement is allowed.
- If
grid[0][0] == 1orgrid[n-1][n-1] == 1, return -1. - Initialize
queue = deque([(0, 0, 1)])(row, col, distance). - Mark
grid[0][0] = 1(visited). - While queue is not empty:
- Pop
(r, c, d). - If
(r, c)is destination, returnd. - Check all 8 neighbors:
- If in bounds and
grid == 0:- Mark
grid = 1and append to queue withd + 1.
- Mark
- If in bounds and
- Pop
- Return -1.
- Time Complexity: O(N^2).
- Space Complexity: O(N^2).
from collections import deque
def shortest_path_binary_matrix(grid):
n = len(grid)
if grid[0][0] or grid[n-1][n-1]: return -1
q = deque([(0, 0, 1)]) # row, col, dist
grid[0][0] = 1 # Mark as visited
while q:
r, c, d = q.popleft()
if r == n - 1 and c == n - 1:
return d
# 8 directions
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
if dr == 0 and dc == 0: continue
nr, nc = r + dr, c + dc
if 0 <= nr < n and 0 <= nc < n and not grid[nr][nc]:
grid[nr][nc] = 1 # Mark visited
q.append((nr, nc, d + 1))
return -1