Skip to content

Commit 23a17e1

Browse files
authored
Create minimum-moves-to-reach-target-with-rotations.py
1 parent ac57988 commit 23a17e1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def minimumMoves(self, grid):
6+
"""
7+
:type grid: List[List[int]]
8+
:rtype: int
9+
"""
10+
level, q, lookup = 0, [(0, 0, False)], set()
11+
while q:
12+
next_q = []
13+
for r, c, is_vertical in q:
14+
if (r, c, is_vertical) in lookup:
15+
continue
16+
if (r, c, is_vertical) == (len(grid)-1, len(grid)-2, False):
17+
return level
18+
lookup.add((r, c, is_vertical))
19+
if not is_vertical:
20+
if c+2 != len(grid[0]) and grid[r][c+2] == 0:
21+
next_q.append((r, c+1, is_vertical))
22+
if r+1 != len(grid) and grid[r+1][c] == 0 and grid[r+1][c+1] == 0:
23+
next_q.append((r+1, c, is_vertical))
24+
next_q.append((r, c, not is_vertical))
25+
else:
26+
if r+2 != len(grid) and grid[r+2][c] == 0:
27+
next_q.append((r+1, c, is_vertical))
28+
if c+1 != len(grid) and grid[r][c+1] == 0 and grid[r+1][c+1] == 0:
29+
next_q.append((r, c+1, is_vertical))
30+
next_q.append((r, c, not is_vertical))
31+
q = next_q
32+
level += 1
33+
return -1

0 commit comments

Comments
 (0)