-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#1091 shortestPathBinaryMatrix.py
65 lines (56 loc) · 1.86 KB
/
#1091 shortestPathBinaryMatrix.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
59
60
61
62
63
64
65
class Solution:
# TLE Solution, using DFS:
# def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
# if grid[0][0] == 1:
# return -1
#
# dirs = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]
# n = len(grid)
#
# def dfs(r: int, c: int, curr: int, visited: set) -> int:
# if not (0 <= r < n and 0 <= c < n):
# return inf
#
# if r == n - 1 and c == n - 1 and grid[r][c] == 0:
# return curr
#
# if grid[r][c] == 1 or (r, c) in visited:
# return inf
#
# v = visited.copy()
# v.add((r, c))
# val = inf
#
# for dr, dc in dirs:
# val = min(val, dfs(r + dr, c + dc, curr + 1, v))
#
# return val
#
# Min = dfs(0, 0, 1, set())
# if Min == inf:
# return -1
#
# return Min
# BFS Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
n = len(grid)
if grid[0][0] != 0:
return -1
if n == 1:
return 1 if grid[0][0] == 0 else -1
dirs = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]
visited = set()
visited.add((0, 0))
q = deque([(0, 0, 1)])
while len(q) > 0:
r, c, d = q.popleft()
for dr, dc in dirs:
new_r, new_c = r + dr, c + dc
if not (0 <= new_r < n and 0 <= new_c < n):
continue
if (new_r, new_c) not in visited and grid[new_r][new_c] == 0:
if new_r == n - 1 and new_c == n - 1:
return d + 1
visited.add((new_r, new_c))
q.append((new_r, new_c, d + 1))
return -1