-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolu.py
More file actions
39 lines (36 loc) · 1.19 KB
/
solu.py
File metadata and controls
39 lines (36 loc) · 1.19 KB
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
class Solution:
# 双向bfs
def openLock(self, deadends: List[str], target: str) -> int:
if target == "0000":
return 0
dead = set(deadends)
if "0000" in dead:
return -1
d = {"0000": 0}
ed = {target: 0}
q = deque(["0000"])
eq = deque([target])
def spin(start: str) -> List[str]:
for i in range(4):
num = int(start[i])
for j in [1, -1]:
yield start[:i] + str((num + j) % 10) + start[i + 1:]
def update(s: deque, visited: dict, u: dict) -> int:
while s:
cur = s.popleft()
tim = t[cur]
for st in spin(cur):
if st not in dead and st not in visited:
if st in u:
return u[st] + tim + 1
else:
s.append(st)
visited[st] = tim + 1
while q and eq:
if len(q) <= len(eq):
r = update(q, d, ed)
else:
r = update(eq, ed, d)
if r:
return r
return -1