Leetcode Problem 2850. Minimum Moves to Spread Stones Over Grid
2850. Minimum Moves to Spread Stones Over Grid
Leetcode Solutions
Backtracking from the zero cells
Collect the positions of all cells with zero stones into a list called zeros.
Define a recursive helper function that takes the current grid state, the list of zero cells, the index of the current zero cell to fill, the current move count, and a reference to the result (minimum moves).
If the index of the current zero cell is equal to the size of the zeros list, update the result with the minimum of the current result and the move count, and return.
Iterate over all cells in the grid, and for each cell with more than one stone, decrement the stone count, increment the stone count of the current zero cell, and recursively call the helper function with the updated parameters.
After the recursive call, backtrack by resetting the stone counts to their original values.
Call the helper function with initial parameters and return the result.