-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2850-MinimumMovesToSpreadStonesOverGrid.go
More file actions
96 lines (88 loc) · 4.41 KB
/
2850-MinimumMovesToSpreadStonesOverGrid.go
File metadata and controls
96 lines (88 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
// 2850. Minimum Moves to Spread Stones Over Grid
// 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:
// <img src="https://assets.leetcode.com/uploads/2023/08/23/example1-3.svg" />
// 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:
// <img src="https://assets.leetcode.com/uploads/2023/08/23/example2-2.svg" />
// 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.
import "fmt"
func minimumMoves(grid [][]int) int {
emptyCells, stoneCells := [][2]int{}, [][2]int{}
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if grid[i][j] == 0 {
emptyCells = append(emptyCells, [2]int{i, j})
} else if grid[i][j] > 1 {
stoneCells = append(stoneCells, [2]int{i, j})
}
}
}
abs := func(x int) int { if x < 0 { return -x; }; return x; }
min := func (x, y int) int { if x < y { return x; }; return y; }
var backtracking func(grid [][]int, emptyCellIdx int) int
backtracking = func(grid [][]int, emptyCellIdx int) int {
if emptyCellIdx == len(emptyCells) { return 0 }
curEmpty := emptyCells[emptyCellIdx]
dist, res := 0, 1 << 32 - 1
for i := 0; i < len(stoneCells); i++ {
take := stoneCells[i]
if grid[take[0]][take[1]] == 1 { continue } // Only take if the grid allows it
d := abs(curEmpty[0]-take[0]) + abs(curEmpty[1]-take[1]) // Calculate manhattan distance
grid[take[0]][take[1]]-- // Accout for taking this stone
dist = d + backtracking(grid, emptyCellIdx+1)
grid[take[0]][take[1]]++
res = min(dist, res)
}
return res
}
return backtracking(grid, 0)
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2023/08/23/example1-3.svg" />
// 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.
fmt.Println(minimumMoves([][]int{{1,1,0},{1,1,1},{1,2,1}})) // 3
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2023/08/23/example2-2.svg" />
// 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.
fmt.Println(minimumMoves([][]int{{1,3,0},{1,0,0},{1,0,3}})) // 4
}