Minimum Moves to Reach Target with Rotations Problem
Description
LeetCode Problem 1210.
In an n*n grid, there is a snake that spans 2 cells and starts moving from the top left corner at (0, 0) and (0, 1). The grid has empty cells represented by zeros and blocked cells represented by ones. The snake wants to reach the lower right corner at (n-1, n-2) and (n-1, n-1).
In one move the snake can:
- Move one cell to the right if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is.
- Move down one cell if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is.
- Rotate clockwise if it's in a horizontal position and the two cells under it are both empty. In that case the snake moves from
(r, c)and(r, c+1)to(r, c)and(r+1, c).

- Rotate counterclockwise if it's in a vertical position and the two cells to its right are both empty. In that case the snake moves from
(r, c)and(r+1, c)to(r, c)and(r, c+1).

Return the minimum number of moves to reach the target.
If there is no way to reach the target, return -1.
Example 1:

Input: grid = [[0,0,0,0,0,1],
[1,1,0,0,1,0],
[0,0,0,0,1,1],
[0,0,1,0,1,0],
[0,1,1,0,0,0],
[0,1,1,0,0,0]]
Output: 11
Explanation:
One possible solution is [right, right, rotate clockwise, right, down, down, down, down, rotate counterclockwise, right, down].
Example 2:
Input: grid = [[0,0,1,1,1,1],
[0,0,0,0,1,1],
[1,1,0,0,0,1],
[1,1,1,0,0,1],
[1,1,1,0,0,1],
[1,1,1,0,0,0]]
Output: 9
Constraints:
2 <= n <= 1000 <= grid[i][j] <= 1- It is guaranteed that the snake starts at empty cells.
Solution
Approach: BFS over Snake Positions
Every move costs one, so the fewest moves is a shortest path, and breadth-first search finds it. A position of the snake is its tail cell (r, c) plus its direction: horizontal (head at (r, c + 1)) or vertical (head at (r + 1, c)). That gives at most 2n2 states, and each has at most three moves.
The moves from a horizontal snake at (r, c), (r, c + 1):
- right: the cell
(r, c + 2)must be empty. - down: both cells below,
(r + 1, c)and(r + 1, c + 1), must be empty. - rotate clockwise to vertical
(r, c),(r + 1, c): the same two cells below must be empty.
The moves from a vertical snake at (r, c), (r + 1, c):
- down: the cell
(r + 2, c)must be empty. - right: both cells to the right,
(r, c + 1)and(r + 1, c + 1), must be empty. - rotate counterclockwise to horizontal
(r, c),(r, c + 1): the same two cells to the right must be empty.
Note that a rotation needs the whole 2x2 square to be free, not just the cell the head moves to.
Start from the horizontal state at (0, 0) and expand level by level, marking states as seen when they are queued. The answer is the level at which we reach the horizontal state with tail (n - 1, n - 2). If the queue runs out first, return -1. The snake never moves left or up, so if it has to go around a wall to the left, the target cannot be reached.
Complexity: O(n2) time and space.
from collections import deque
class Solution:
def minimumMoves(self, grid: List[List[int]]) -> int:
n = len(grid)
def is_free(r: int, c: int) -> bool:
return r < n and c < n and grid[r][c] == 0
# A state is the tail cell (r, c) and the direction d: 0 = horizontal (head at (r, c + 1)),
# 1 = vertical (head at (r + 1, c)).
start, target = (0, 0, 0), (n - 1, n - 2, 0)
seen = {start}
queue = deque([start])
moves = 0
while queue:
for _ in range(len(queue)):
state = queue.popleft()
if state == target:
return moves
r, c, d = state
nxt = []
if d == 0:
if is_free(r, c + 2):
nxt.append((r, c + 1, 0)) # right
if is_free(r + 1, c) and is_free(r + 1, c + 1):
nxt.append((r + 1, c, 0)) # down
nxt.append((r, c, 1)) # rotate clockwise
else:
if is_free(r + 2, c):
nxt.append((r + 1, c, 1)) # down
if is_free(r, c + 1) and is_free(r + 1, c + 1):
nxt.append((r, c + 1, 1)) # right
nxt.append((r, c, 0)) # rotate counterclockwise
for s in nxt:
if s not in seen:
seen.add(s)
queue.append(s)
moves += 1
return -1