Updated: 2024-03-12
1 min read
On This Page
class Solution:
def shortestDistance(
self, maze: List[List[int]], start: List[int], destination: List[int]
) -> int:
m, n = len(maze), len(maze[0])
dirs = (-1, 0, 1, 0, -1)
si, sj = start
di, dj = destination
q = deque([(si, sj)])
dist = [[inf] * n for _ in range(m)]
dist[si][sj] = 0
while q:
i, j = q.popleft()
for a, b in pairwise(dirs):
x, y, k = i, j, dist[i][j]
while 0 <= x + a < m and 0 <= y + b < n and maze[x + a][y + b] == 0:
x, y, k = x + a, y + b, k + 1
if k < dist[x][y]:
dist[x][y] = k
q.append((x, y))
return -1 if dist[di][dj] == inf else dist[di][dj]