-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMinimum Moves to Reach Target with Rotations.py
30 lines (29 loc) · 1.38 KB
/
Minimum Moves to Reach Target with Rotations.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
class Solution:
def minimumMoves(self, grid: List[List[int]]) -> int:
queue , vis , n = [(0,1,0,0)] , {} , len(grid)
while queue:
x,y,pos,moves = queue.pop(0)
if x == y == n-1 and pos == 0: return moves
if pos == 0:
if y + 1 < n and grid[x][y+1] == 0 and (x,y+1,0) not in vis:
vis[(x,y+1,0)] = True
queue.append((x,y+1,0,moves+1))
if x + 1 < n and grid[x+1][y-1] == 0 and grid[x+1][y] == 0:
if (x+1,y-1,1) not in vis:
vis[(x+1,y-1,1)] = True
queue.append((x+1,y-1,1,moves+1))
if (x+1,y,0) not in vis:
vis[(x+1,y,0)] = True
queue.append((x+1,y,0,moves+1))
else:
if x + 1 < n and grid[x+1][y] == 0 and (x+1,y,1) not in vis:
vis[(x+1,y,1)] = True
queue.append((x+1,y,1,moves+1))
if y + 1 < n and grid[x-1][y+1] == grid[x][y+1] == 0:
if (x-1,y+1,0) not in vis:
vis[(x-1,y+1,0)] = True
queue.append((x-1,y+1,0,moves+1))
if (x,y+1,1) not in vis:
vis[(x,y+1,1)] = True
queue.append((x,y+1,1,moves+1))
return -1