forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
51 lines (49 loc) · 1.41 KB
/
Solution.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
# """
# This is GridMaster's API interface.
# You should not implement it, or speculate about its implementation
# """
# class GridMaster(object):
# def canMove(self, direction: str) -> bool:
#
#
# def move(self, direction: str) -> bool:
#
#
# def isTarget(self) -> None:
#
#
class Solution(object):
def findShortestPath(self, master: 'GridMaster') -> int:
def dfs(i, j):
nonlocal target
if master.isTarget():
target = (i, j)
for dir, ndir, a, b in dirs:
x, y = i + a, j + b
if master.canMove(dir) and (x, y) not in s:
s.add((x, y))
master.move(dir)
dfs(x, y)
master.move(ndir)
target = None
s = set()
dirs = [['U', 'D', -1, 0], ['D', 'U', 1, 0],
['L', 'R', 0, -1], ['R', 'L', 0, 1]]
dfs(0, 0)
if target is None:
return -1
s.remove((0, 0))
q = deque([(0, 0)])
ans = -1
while q:
ans += 1
for _ in range(len(q)):
i, j = q.popleft()
if (i, j) == target:
return ans
for _, _, a, b in dirs:
x, y = i + a, j + b
if (x, y) in s:
s.remove((x, y))
q.append((x, y))
return -1