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.
In one move, you can move a single stone from its current cell to any other cell if the two cells share a side.
Return the minimum number of moves required to place one stone in each cell.
Example 1:
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:
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.
Example 2:
Input: grid = [[1,3,0],[1,0,0],[1,0,3]]
Output: 4
Explanation: 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.
Constraints:
grid.length == grid[i].length == 3
0 <= grid[i][j] <= 9
Sum of grid is equal to 9.
Solutions
Solution 1: Naive BFS
The problem is essentially finding the shortest path from the initial state to the target state in a state graph, so we can use BFS to solve it. The initial state is grid, and the target state is [[1, 1, 1], [1, 1, 1], [1, 1, 1]]. In each operation, we can move a stone greater than \(1\) from a cell to an adjacent cell that does not exceed \(1\). If the target state is found, we can return the current layer number, which is the minimum number of moves.
We can put all the coordinates \((i, j)\) of cells with a value of \(0\) into an array \(left\). If the value \(v\) of a cell is greater than \(1\), we put \(v-1\) coordinates \((i, j)\) into an array \(right\). The problem then becomes that each coordinate \((i, j)\) in \(right\) needs to be moved to a coordinate \((x, y)\) in \(left\), and we need to find the minimum number of moves.
Let's denote the length of \(left\) as \(n\). We can use an \(n\)-bit binary number to represent whether each coordinate in \(left\) is filled by a coordinate in \(right\), where \(1\) represents being filled, and \(0\) represents not being filled. Initially, \(f[i] = \infty\), and the rest \(f[0]=0\).
Consider \(f[i]\), let the number of \(1\)s in the binary representation of \(i\) be \(k\). We enumerate \(j\) in the range \([0..n)\), if the \(j\)th bit of \(i\) is \(1\), then \(f[i]\) can be transferred from \(f[i \oplus (1 << j)]\), and the cost of the transfer is \(cal(left[k-1], right[j])\), where \(cal\) represents the Manhattan distance between two coordinates. The final answer is \(f[(1 << n) - 1]\).
The time complexity is \(O(n \times 2^n)\), and the space complexity is \(O(2^n)\). Here, \(n\) is the length of \(left\), and in this problem, \(n \le 9\).