Minimum Moves to Spread Stones Over Grid — LeetCode 2850 Python Solution

MediumBreadth-First SearchArrayDynamic ProgrammingMatrix
Problem
#2850
Reading time
5 min

The problem

You are given a 0-indexed 2D integer matrix grid of size 3 * 3, representing the number of stones in each cell. The grid contains exactly 9 stones, and there can be multiple stones in a single cell.

Example

Input
grid = [[1,1,0],[1,1,1],[1,2,1]]
Output
3
Explanation
One possible sequence of moves to place one stone in each cell is:

Python solution

Python
class Solution:
    def minimumMoves(self, grid: List[List[int]]) -> int:
        q = deque([tuple(tuple(row) for row in grid)])
        vis = set(q)
        ans = 0
        dirs = (-1, 0, 1, 0, -1)
        while 1:
            for _ in range(len(q)):
                cur = q.popleft()
                if all(x for row in cur for x in row):
                    return ans
                for i in range(3):
                    for j in range(3):
                        if cur[i][j] > 1:
                            for a, b in pairwise(dirs):
                                x, y = i + a, j + b
                                if 0 <= x < 3 and 0 <= y < 3 and cur[x][y] < 2:
                                    nxt = [list(row) for row in cur]
                                    nxt[i][j] -= 1
                                    nxt[x][y] += 1
                                    nxt = tuple(tuple(row) for row in nxt)
                                    if nxt not in vis:
                                        vis.add(nxt)
                                        q.append(nxt)
            ans += 1

Complexity

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

Pattern: Matrix and Grid

Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 2850. Minimum Moves to Spread Stones Over Grid 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 2850. Minimum Moves to Spread Stones Over Grid?
LeetCode 2850. Minimum Moves to Spread Stones Over Grid is rated Medium on LeetCode.
What is the time complexity of LeetCode 2850. Minimum Moves to Spread Stones Over Grid?
The Python solution on this page runs in O(n \times 2^n).
What is the space complexity of LeetCode 2850. Minimum Moves to Spread Stones Over Grid?
The Python solution on this page uses O(2^n) auxiliary space.
What topics does LeetCode 2850. Minimum Moves to Spread Stones Over Grid cover?
LeetCode 2850. Minimum Moves to Spread Stones Over Grid is tagged Breadth-First Search, Array, Dynamic Programming 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