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

<p>In one move, you can move a single stone from its current cell to any other cell if the two cells share a side.</p>

<p>Return <em>the <strong>minimum number of moves</strong> required to place one stone in each cell</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/23/example1-3.svg" style="width: 401px; height: 281px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,0],[1,1,1],[1,2,1]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One possible sequence of moves to place one stone in each cell is: 
1- Move one stone from cell (2,1) to cell (2,2).
2- Move one stone from cell (2,2) to cell (1,2).
3- Move one stone from cell (1,2) to cell (0,2).
In total, it takes 3 moves to place one stone in each cell of the grid.
It can be shown that 3 is the minimum number of moves required to place one stone in each cell.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/23/example2-2.svg" style="width: 401px; height: 281px;" />
<pre>
<strong>Input:</strong> grid = [[1,3,0],[1,0,0],[1,0,3]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible sequence of moves to place one stone in each cell is:
1- Move one stone from cell (0,1) to cell (0,2).
2- Move one stone from cell (0,1) to cell (1,1).
3- Move one stone from cell (2,2) to cell (1,2).
4- Move one stone from cell (2,2) to cell (2,1).
In total, it takes 4 moves to place one stone in each cell of the grid.
It can be shown that 4 is the minimum number of moves required to place one stone in each cell.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>grid.length == grid[i].length == 3</code></li>
	<li><code>0 &lt;= grid[i][j] &lt;= 9</code></li>
	<li>Sum of <code>grid</code> is equal to <code>9</code>.</li>
</ul>

from collections import deque

class Solution:
    def minimumMoves(self, grid: List[List[int]]) -> int:
        # Flatten the 2D grid into a 1D list
        start = [stone for row in grid for stone in row]
        
        # Goal state: each cell has exactly one stone
        goal = [1] * 9
        
        # Directions: moves a stone can make (up, down, left, right)
        directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
        
        # Convert 1D index to 2D index
        def to_2d(index):
            return divmod(index, 3)
        
        # Convert 2D index to 1D index
        def to_1d(row, col):
            return row * 3 + col
        
        # Check if a move is valid
        def is_valid(row, col):
            return 0 <= row < 3 and 0 <= col < 3
        
        # BFS to find the minimum number of moves
        queue = deque([(start, 0)])  # (state, moves)
        visited = set()
        
        while queue:
            current, moves = queue.popleft()
            
            if current == goal:
                return moves
            
            if tuple(current) in visited:
                continue
            
            visited.add(tuple(current))
            
            # Find the index of the first cell with more than one stone
            index = next((i for i, stone in enumerate(current) if stone > 1), None)
            if index is None:
                continue
            
            row, col = to_2d(index)
            
            # Try moving the stone in each direction
            for dr, dc in directions:
                new_row, new_col = row + dr, col + dc
                if is_valid(new_row, new_col):
                    new_index = to_1d(new_row, new_col)
                    new_state = current[:]
                    new_state[index] -= 1
                    new_state[new_index] += 1
                    queue.append((new_state, moves + 1))
        
        return -1  # If no solution found

# Example usage:
solution = Solution()
grid = [[1,1,0],[1,1,1],[1,2,1]]
print(solution.minimumMoves(grid))  # Output: 3
17200