2850. Minimum Moves to Spread Stones Over Grid Medium

@problem@discussion
#Array#Dynamic Programming#Breadth-First Search#Matrix



1/**
2 * [2850] Minimum Moves to Spread Stones Over Grid
3 *
4 * 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.
5 * In one move, you can move a single stone from its current cell to any other cell if the two cells share a side.
6 * Return the minimum number of moves required to place one stone in each cell.
7 *  
8 * <strong class="example">Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2023/08/23/example1-3.svg" style="width: 401px; height: 281px;" />
10 * Input: grid = [[1,1,0],[1,1,1],[1,2,1]]
11 * Output: 3
12 * Explanation: One possible sequence of moves to place one stone in each cell is: 
13 * 1- Move one stone from cell (2,1) to cell (2,2).
14 * 2- Move one stone from cell (2,2) to cell (1,2).
15 * 3- Move one stone from cell (1,2) to cell (0,2).
16 * In total, it takes 3 moves to place one stone in each cell of the grid.
17 * It can be shown that 3 is the minimum number of moves required to place one stone in each cell.
18 * 
19 * <strong class="example">Example 2:
20 * <img alt="" src="https://assets.leetcode.com/uploads/2023/08/23/example2-2.svg" style="width: 401px; height: 281px;" />
21 * Input: grid = [[1,3,0],[1,0,0],[1,0,3]]
22 * Output: 4
23 * Explanation: One possible sequence of moves to place one stone in each cell is:
24 * 1- Move one stone from cell (0,1) to cell (0,2).
25 * 2- Move one stone from cell (0,1) to cell (1,1).
26 * 3- Move one stone from cell (2,2) to cell (1,2).
27 * 4- Move one stone from cell (2,2) to cell (2,1).
28 * In total, it takes 4 moves to place one stone in each cell of the grid.
29 * It can be shown that 4 is the minimum number of moves required to place one stone in each cell.
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	grid.length == grid[i].length == 3
35 * 	0 <= grid[i][j] <= 9
36 * 	Sum of grid is equal to 9.
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/minimum-moves-to-spread-stones-over-grid/
42// discuss: https://leetcode.com/problems/minimum-moves-to-spread-stones-over-grid/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn minimum_moves(grid: Vec<Vec<i32>>) -> i32 {
48        0
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_2850() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.