Minimum Moves to Reach Target with Rotations — LeetCode 1210 Python Solution

HardBreadth-First SearchArrayMatrix
Problem
#1210
Reading time
6 min

The problem

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.

Example

Input
grid = [[0,0,0,0,0,1],
Output
11
Explanation
One possible solution is [right, right, rotate clockwise, right, down, down, down, down, rotate counterclockwise, right, down].

Python solution

Python
class Solution:
    def minimumMoves(self, grid: List[List[int]]) -> int:
        def move(i1, j1, i2, j2):
            if 0 <= i1 < n and 0 <= j1 < n and 0 <= i2 < n and 0 <= j2 < n:
                a, b = i1 * n + j1, i2 * n + j2
                status = 0 if i1 == i2 else 1
                if (a, status) not in vis and grid[i1][j1] == 0 and grid[i2][j2] == 0:
                    q.append((a, b))
                    vis.add((a, status))

        n = len(grid)
        target = (n * n - 2, n * n - 1)
        q = deque([(0, 1)])
        vis = {(0, 0)}
        ans = 0
        while q:
            for _ in range(len(q)):
                a, b = q.popleft()
                if (a, b) == target:
                    return ans
                i1, j1 = a // n, a % n
                i2, j2 = b // n, b % n
                # 尝试向右平移(保持身体水平/垂直状态)
                move(i1, j1 + 1, i2, j2 + 1)
                # 尝试向下平移(保持身体水平/垂直状态)
                move(i1 + 1, j1, i2 + 1, j2)
                # 当前处于水平状态,且 grid[i1 + 1][j2] 无障碍,尝试顺时针旋转90°
                if i1 == i2 and i1 + 1 < n and grid[i1 + 1][j2] == 0:
                    move(i1, j1, i1 + 1, j1)
                # 当前处于垂直状态,且 grid[i2][j1 + 1] 无障碍,尝试逆时针旋转90°
                if j1 == j2 and j1 + 1 < n and grid[i2][j1 + 1] == 0:
                    move(i1, j1, i1, j1 + 1)
            ans += 1
        return -1

Complexity

MeasureComplexity
TimeO(n^2)
SpaceO(n^2) auxiliary

Pattern: Matrix and Grid

Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 1210. Minimum Moves to Reach Target with Rotations is filed here because LeetCode tags it Matrix, which is the vocabulary this hub collects.

The matrix and grid guide has the Python template for the pattern and the 216 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1210. Minimum Moves to Reach Target with Rotations?
LeetCode 1210. Minimum Moves to Reach Target with Rotations is rated Hard on LeetCode.
What is the time complexity of LeetCode 1210. Minimum Moves to Reach Target with Rotations?
The Python solution on this page runs in O(n^2).
What is the space complexity of LeetCode 1210. Minimum Moves to Reach Target with Rotations?
The Python solution on this page uses O(n^2) auxiliary space.
What topics does LeetCode 1210. Minimum Moves to Reach Target with Rotations cover?
LeetCode 1210. Minimum Moves to Reach Target with Rotations is tagged Breadth-First Search, Array and Matrix on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview