Minimum Moves to Reach Target with Rotations - Problem

In an n×n grid, there is a snake that spans 2 cells and starts at the top-left corner at positions (0, 0) and (0, 1). The grid has empty cells represented by 0 and blocked cells represented by 1.

The snake wants to reach the target at the lower-right corner at positions (n-1, n-2) and (n-1, n-1).

In one move, the snake can:

  • Move right: Move one cell to the right if there are no blocked cells there
  • Move down: Move one cell down if there are no blocked cells there
  • Rotate clockwise: If in horizontal position and the two cells under it are both empty, snake moves from (r, c), (r, c+1) to (r, c), (r+1, c)
  • Rotate counterclockwise: If in vertical position and the two cells to its right are both empty, snake moves from (r, c), (r+1, c) to (r, c), (r, c+1)

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

Input & Output

Example 1 — Basic Case
$ 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
💡 Note: Snake starts at (0,0)-(0,1) horizontal and needs to reach (5,4)-(5,5). Through a series of moves and rotations, minimum 11 moves are needed.
Example 2 — Simple Grid
$ 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,0],[0,1,1,0,0,0],[0,1,1,0,0,0]]
Output: -1
💡 Note: Snake cannot reach the target due to blocked paths, so return -1.
Example 3 — Small Grid
$ Input: grid = [[0,0,0],[0,1,0],[0,0,0]]
Output: 4
💡 Note: In a 3×3 grid, snake needs 4 moves: right, rotate, down, rotate to reach (2,1)-(2,2).

Constraints

  • 2 ≤ n ≤ 100
  • grid.length == n
  • grid[i].length == n
  • grid[i][j] is 0 or 1

Visualization

Tap to expand
Minimum Moves to Reach Target with Rotations INPUT 6x6 Grid (0=empty, 1=blocked) 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 Blue = Start (snake) Green = Target Red = Blocked Snake Moves: Right, Down, Rotate CW/CCW Snake spans 2 cells ALGORITHM (BFS) 1 Initialize BFS Queue: (pos, orientation, moves) Start: ((0,0),(0,1), H, 0) 2 State: (row, col, orientation) H=horizontal, V=vertical Track visited states 3 Process Moves Try: right, down, rotate Check validity each move 4 Check Target Target: (n-1,n-2),(n-1,n-1) Horizontal at bottom-right BFS Queue Processing 0,0,H 0,1,H ... Level by level exploration First to reach target = min moves O(n^2) time and space FINAL RESULT Optimal Path Found Orange = Path taken Move Breakdown: Right moves: 4 Down moves: 5 Rotations: 2 Total: 11 Output: 11 OK - Minimum moves found! Key Insight: BFS guarantees minimum moves because it explores all states at distance d before states at distance d+1. The state is defined by (tail_row, tail_col, orientation) - this captures the snake's full position. Rotations require checking a 2x2 area is empty, making the problem more complex than simple pathfinding. TutorialsPoint - Minimum Moves to Reach Target with Rotations | BFS Approach
Asked in
Google 15 Facebook 12 Amazon 8
32.0K Views
Medium Frequency
~35 min Avg. Time
853 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen