forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid-teleportation-traversal.py
More file actions
43 lines (40 loc) · 1.37 KB
/
grid-teleportation-traversal.py
File metadata and controls
43 lines (40 loc) · 1.37 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
40
41
42
43
# Time: O(m * n)
# Space: O(m * n)
import collections
# 0-1 bfs
class Solution(object):
def minMoves(self, matrix):
"""
:type matrix: List[str]
:rtype: int
"""
DIRECTIONS = [(0, -1), (0, 1), (-1, 0), (1, 0)]
m, n = len(matrix), len(matrix[0])
lookup = [[] for _ in xrange(26)]
for i in xrange(m):
for j in xrange(n):
if matrix[i][j] in ".#":
continue
lookup[ord(matrix[i][j])-ord('A')].append((i, j))
lookup2 = [[False]*len(matrix[0]) for _ in xrange(m)]
dq = collections.deque([(0, 0, 0)])
while dq:
step, i, j = dq.popleft()
if lookup2[i][j]:
continue
lookup2[i][j] = True
if (i, j) == (m-1, n-1):
return step
for di, dj in DIRECTIONS:
ni, nj = i+di, j+dj
if not (0 <= ni < m and 0 <= nj < n and matrix[ni][nj] != '#' and not lookup2[ni][nj]):
continue
dq.append((step+1, ni, nj))
if matrix[i][j] == '.':
continue
for ni, nj in lookup[ord(matrix[i][j])-ord('A')]:
if lookup2[ni][nj]:
continue
dq.appendleft((step, ni, nj))
lookup[ord(matrix[i][j])-ord('A')] = []
return -1