-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlc752. Open the Lock.py
42 lines (35 loc) · 1.05 KB
/
lc752. Open the Lock.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
class Solution:
def openLock(self, deadends, target):
"""
:type deadends: List[str]
:type target: str
:rtype: int
"""
deadset = set(deadends)
if '0000' in deadset:
return -1
import queue
setps = 0
q = queue.Queue()
q.put('0000')
visited = {'0000'}
while not q.empty():
setps += 1
length = q.qsize()
for _ in range(length):
node = q.get()
for i in range(4):
for j in (-1, 1):
slst = list(node)
slst[i] = str((int(slst[i]) + j) % 10)
tem = ''.join(slst)
if tem == target:
return setps
if tem not in visited and tem not in deadset:
q.put(tem)
visited.add(tem)
return -1
deadends = ["0000"]
target = "8888"
s = Solution()
print(s.openLock(deadends, target))