-
Notifications
You must be signed in to change notification settings - Fork 0
/
1091-shortest-path-in-binary-matrix.py
40 lines (33 loc) · 1.19 KB
/
1091-shortest-path-in-binary-matrix.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
from collections import deque
class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
if grid[0][0] == 1:
return -1
nR = len(grid)
nC = len(grid[0])
def adjs(r, c):
buf = []
for rx, cx in [(r, c + 1), (r - 1, c + 1),
(r - 1, c), (r - 1, c - 1),
(r, c - 1), (r + 1, c - 1),
(r + 1, c), (r + 1, c + 1)
]:
if rx in range(0, nR) and cx in range(0, nC):
if grid[rx][cx] == 0:
buf.append((rx, cx))
return buf
dq = deque([(0, 0)])
vrec = set([(0, 0)])
ctr = 0
while dq:
N = len(dq)
for ix in range(N):
rr, cc = dq.popleft()
if rr == (nR - 1) and cc == (nC - 1):
return 1 + ctr
for adj in adjs(rr, cc):
if adj not in vrec:
vrec.add(adj)
dq.append(adj)
ctr = ctr + 1
return -1