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:
(r, c) and (r, c+1) to (r, c) and (r+1, c).
(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] <= 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are holding a long stick that takes up two squares on a giant game board, and you need to slide and roll it until it reaches a specific target spot. The brute force approach works by trying every single possible sequence of movements and rotations, step by step, until you successfully navigate the stick to the winning position.
Here's how the algorithm would work step-by-step:
class SnakeGameBruteForce:
def minimum_moves(self, grid):
board_size = len(grid)
target_position = (board_size - 1, board_size - 2, board_size - 1, board_size - 1)
# Use a queue for exhaustive level-by-level exploration to find the shortest path
exploration_queue = [(0, 0, 0, 1, 0)]
visited_positions = set()
visited_positions.add((0, 0, 0, 1))
while exploration_queue:
row_head, col_head, row_tail, col_tail, total_moves = exploration_queue.pop(0)
if (row_head, col_head, row_tail, col_tail) == target_position:
return total_moves
current_orientation = 0 if row_head == row_tail else 1
if current_orientation == 0:
if col_head + 1 < board_size and grid[row_head][col_head + 1] == 0 and grid[row_tail][col_tail + 1] == 0:
next_state = (row_head, col_head + 1, row_tail, col_tail + 1)
if next_state not in visited_positions:
visited_positions.add(next_state)
exploration_queue.append((next_state[0], next_state[1], next_state[2], next_state[3], total_moves + 1))
if row_head + 1 < board_size and grid[row_head + 1][col_head] == 0 and grid[row_tail + 1][col_tail] == 0:
next_state = (row_head + 1, col_head, row_tail + 1, col_tail)
if next_state not in visited_positions:
visited_positions.add(next_state)
exploration_queue.append((next_state[0], next_state[1], next_state[2], next_state[3], total_moves + 1))
if row_head + 1 < board_size and grid[row_head + 1][col_head] == 0 and grid[row_head + 1][col_tail] == 0:
next_state = (row_head, col_head, row_head + 1, col_head)
if next_state not in visited_positions:
visited_positions.add(next_state)
exploration_queue.append((next_state[0], next_state[1], next_state[2], next_state[3], total_moves + 1))
else:
if row_head + 1 < board_size and grid[row_head + 1][col_head] == 0 and grid[row_tail + 1][col_tail] == 0:
next_state = (row_head + 1, col_head, row_tail + 1, col_tail)
if next_state not in visited_positions:
visited_positions.add(next_state)
exploration_queue.append((next_state[0], next_state[1], next_state[2], next_state[3], total_moves + 1))
if col_head + 1 < board_size and grid[row_head][col_head + 1] == 0 and grid[row_tail][col_tail + 1] == 0:
next_state = (row_head, col_head + 1, row_tail, col_tail + 1)
if next_state not in visited_positions:
visited_positions.add(next_state)
exploration_queue.append((next_state[0], next_state[1], next_state[2], next_state[3], total_moves + 1))
if col_head + 1 < board_size and grid[row_head][col_head + 1] == 0 and grid[row_tail][col_tail + 1] == 0:
next_state = (row_head, col_head, row_head, col_head + 1)
if next_state not in visited_positions:
visited_positions.add(next_state)
exploration_queue.append((next_state[0], next_state[1], next_state[2], next_state[3], total_moves + 1))
# Terminate search and return negative one if no valid path exists to target
return -1To find the shortest path for a two-unit-long snake across a grid, we can use a systematic exploration strategy that finds the absolute fewest moves. Instead of guessing, we test moves level by level, ensuring that the very first time our snake reaches the target, we are guaranteed to have taken the minimum number of steps.
Here's how the algorithm would work step-by-step:
from collections import deque
def minimum_moves(grid):
grid_dimension = len(grid)
tail_row, tail_column, head_row, head_column = 0, 0, 0, 1
visited_configurations = set()
# Using breadth-first search guarantees finding the shortest path level by level.
exploration_queue = deque([(tail_row, tail_column, head_row, head_column, 0)])
visited_configurations.add((tail_row, tail_column, head_row, head_column))
while exploration_queue:
current_tail_row, current_tail_column, current_head_row, current_head_column, move_count = exploration_queue.popleft()
if current_tail_row == grid_dimension - 1 and current_tail_column == grid_dimension - 2 and current_head_row == grid_dimension - 1 and current_head_column == grid_dimension - 1:
return move_count
is_horizontal = (current_tail_row == current_head_row)
if is_horizontal:
if current_head_column + 1 < grid_dimension and grid[current_head_row][current_head_column + 1] == 0:
next_configuration = (current_tail_row, current_tail_column + 1, current_head_row, current_head_column + 1)
if next_configuration not in visited_configurations:
visited_configurations.add(next_configuration)
exploration_queue.append((next_configuration[0], next_configuration[1], next_configuration[2], next_configuration[3], move_count + 1))
if current_head_row + 1 < grid_dimension and grid[current_tail_row + 1][current_tail_column] == 0 and grid[current_head_row + 1][current_head_column] == 0:
next_configuration = (current_tail_row, current_tail_column, current_head_row + 1, current_head_column)
if next_configuration not in visited_configurations:
visited_configurations.add(next_configuration)
exploration_queue.append((next_configuration[0], next_configuration[1], next_configuration[2], next_configuration[3], move_count + 1))
# Rotating clockwise requires checking the adjacent cell to ensure no obstacles block the pivot.
next_rotation = (current_tail_row, current_tail_column, current_tail_row + 1, current_tail_column)
if next_rotation not in visited_configurations:
visited_configurations.add(next_rotation)
exploration_queue.append((next_rotation[0], next_rotation[1], next_rotation[2], next_rotation[3], move_count + 1))
else:
if current_head_row + 1 < grid_dimension and grid[current_head_row + 1][current_head_column] == 0:
next_configuration = (current_tail_row + 1, current_tail_column, current_head_row + 1, current_head_column)
if next_configuration not in visited_configurations:
visited_configurations.add(next_configuration)
exploration_queue.append((next_configuration[0], next_configuration[1], next_configuration[2], next_configuration[3], move_count + 1))
if current_head_column + 1 < grid_dimension and grid[current_tail_row][current_tail_column + 1] == 0 and grid[current_head_row][current_head_column + 1] == 0:
next_configuration = (current_tail_row, current_tail_column, current_head_row, current_head_column + 1)
if next_configuration not in visited_configurations:
visited_configurations.add(next_configuration)
exploration_queue.append((next_configuration[0], next_configuration[1], next_configuration[2], next_configuration[3], move_count + 1))
# Rotating counter-clockwise requires verifying the side cell is clear before turning.
next_rotation = (current_tail_row, current_tail_column, current_tail_row, current_tail_column + 1)
if next_rotation not in visited_configurations:
visited_configurations.add(next_rotation)
exploration_queue.append((next_rotation[0], next_rotation[1], next_rotation[2], next_rotation[3], move_count + 1))
return -1| Case | How to Handle |
|---|---|
| Grid size n is smaller than the snake length of 2 | The algorithm should immediately return -1 since a 1x1 grid cannot accommodate the snake. |
| Start position or target position is blocked by an obstacle | The search will find zero valid paths and correctly return -1. |
| No valid path exists to reach the target due to surrounding walls | The BFS queue will empty without reaching the target state, resulting in a return value of -1. |
| Snake is blocked from rotating by adjacent obstacles | Rotation validation checks all cells involved in the pivot and returns false if any are blocked. |
| Maximum grid size of n equals 100 | State space is bounded and visited sets prevent infinite loops, allowing BFS to scale efficiently within time limits. |
| Revisiting the same snake state with different orientations or positions | A 3D visited array tracking row, column, and orientation prevents redundant computations and handles graph cycles. |