Minimum Moves to Reach Target with Rotations
Hard · Array, Breadth-First Search, Matrix
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:
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], Output: 11
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], Output: 9
Constraints
- 2 <= n <= 100
- 0 <= grid[i][j] <= 1
- It is guaranteed that the snake starts at empty cells.
Starter code (python)
class Solution(object):
def minimumMoves(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
Related problems
- Two Sum (Easy)
- Median of Two Sorted Arrays (Hard)
- Container With Most Water (Medium)
Previous: Get Equal Substrings Within Budget · Next: Where Will the Ball Fall · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.