-
Notifications
You must be signed in to change notification settings - Fork 481
/
1293.py
28 lines (24 loc) · 957 Bytes
/
1293.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
class Solution:
def shortestPath(self, g: List[List[int]], k: int) -> int:
r, c = len(g), len(g[0])
if k >= r + c - 3:
return r + c -2
mem = {(0, 0):0}
q, step = [(0, 0, 0)], 0
while q:
n = len(q)
for _ in range(n):
x, y, pre = q.pop(0)
if pre > k:
continue
if x == r - 1 and y == c - 1:
return step
for i, j in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
nx, ny = x + i, y + j
if 0 <= nx < r and 0 <= ny < c:
nPre = pre + 1 if g[nx][ny] == 1 else pre
if nPre < mem.get((nx, ny), float("inf")):
q.append((nx, ny, nPre))
mem[(nx, ny)] = nPre
step += 1
return -1