You are given an grid, where each cell contains either
A snake of length
In each move, the snake can perform one of the following actions:
Move one cell to the right:
If the snake is horizontal, both cells must be shifted right by
If the snake is vertical, both cells must be shifted right by
Move one cell down:
If the snake is horizontal, both cells must be shifted down by
If the snake is vertical, both cells must be shifted down by
Rotate clockwise:
If the snake is horizontal, it can rotate clockwise around its tail to become vertical, occupying
Rotate counterclockwise:
If the snake is vertical, it can rotate counterclockwise around its tail to become horizontal, occupying
Return the minimum number of moves required to reach the target position, or
Constraints:
grid.length
grid[i].length
grid[i][j]
It is guaranteed that the snake starts at empty cells.
You are given an grid, where each cell contains either
A snake of length
In each move, the snake can perform one of the following actions:
Move one cell to the right:
If the snake is horizontal, both cells must be shifted right by
If the snake is vertical, both cells must be shifted right by
Move one cell down:
If the snake is horizontal, both cells must be shifted down by
If the snake is vertical, both cells must be shifted down by
Rotate clockwise:
If the snake is horizontal, it can rotate clockwise around its tail to become vertical, occupying
Rotate counterclockwise:
If the snake is vertical, it can rotate counterclockwise around its tail to become horizontal, occupying
Return the minimum number of moves required to reach the target position, or
Constraints:
grid.length
grid[i].length
grid[i][j]
It is guaranteed that the snake starts at empty cells.