-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsolution.go
36 lines (33 loc) · 1014 Bytes
/
solution.go
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
func minCost(grid [][]int) int {
m, n := len(grid), len(grid[0])
directions := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
cost := make([][]int, m)
for i := range cost {
cost[i] = make([]int, n)
for j := range cost[i] {
cost[i][j] = 1 << 30 // Max int
}
}
cost[0][0] = 0
dq := [][]int{{0, 0}}
for len(dq) > 0 {
x, y := dq[0][0], dq[0][1]
dq = dq[1:]
for i, d := range directions {
nx, ny := x+d[0], y+d[1]
newCost := cost[x][y]
if grid[x][y] != i+1 {
newCost++
}
if nx >= 0 && ny >= 0 && nx < m && ny < n && newCost < cost[nx][ny] {
cost[nx][ny] = newCost
if grid[x][y] == i+1 {
dq = append([][]int{{nx, ny}}, dq...)
} else {
dq = append(dq, []int{nx, ny})
}
}
}
}
return cost[m-1][n-1]
}