-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_shortest_path.py
58 lines (42 loc) · 1.51 KB
/
search_shortest_path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#
# 1091. Shortest Path in Binary Matrix
# https://leetcode.com/problems/shortest-path-in-binary-matrix/
#
from collections import deque
def shortestPath(grid: list[list[int]]) -> int:
"""To find the length of shortest path from top left to bottom right.
there are various trajectories from each step, to count the smallest level
X) DFS
target trajectories are unknown
1) BFS
find out all cells that can possibly be visited by N steps
then as soon as destination can be reached, return steps
* this avoided going backwards
time complexity: O(N^2), space complexity: O(N^2)
"""
if (grid[0][0], grid[-1][-1]) != (0, 0):
return -1
N = len(grid)
OFFSETS = [(i, j) for i in [-1, 0, 1] for j in [-1, 0, 1] if (i, j) != (0, 0)]
queue, visited, length = deque([(0, 0)]), set((0, 0)), 1
while queue:
for _ in range(len(queue)):
# { search from }
x, y = queue.popleft()
# { target condition }
if (x, y) == (N - 1, N - 1):
return length
# { search options }
for dx, dy in OFFSETS:
nx, ny = x + dx, y + dy
# { search conditions }
if (
0 <= nx < N
and 0 <= ny < N
and (nx, ny) not in visited
and grid[nx][ny] == 0
):
queue.append((nx, ny))
visited.add((nx, ny))
length += 1
return -1