-
Notifications
You must be signed in to change notification settings - Fork 481
/
1293.go
48 lines (43 loc) · 1.19 KB
/
1293.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
37
38
39
40
41
42
43
44
45
46
47
48
func shortestPath(g [][]int, k int) int {
r, c := len(g), len(g[0])
if k >= r + c - 3 {
return r + c - 2
}
mem := make([][]int, r)
for i := 0; i < r; i++ {
mem[i] = make([]int, c)
for j := 0; j < c; j++ {
mem[i][j] = 10000
}
}
q := [][]int{{0, 0, 0}}
mem[0][0] = 0
d, step := []int{0, 1, 0, -1, 0}, 0
for len(q) != 0 {
for n := len(q); n > 0; n-- {
pre := q[0]
q = q[1:]
if pre[2] > k {
continue
}
if pre[0] == r - 1 && pre[1] == c - 1 {
return step
}
for i := 0; i < 4; i++ {
nx, ny := pre[0] + d[i], pre[1] + d[i + 1]
if nx >= 0 && nx < r && ny >= 0 && ny < c {
nPre := pre[2]
if g[nx][ny] == 1 {
nPre++
}
if nPre < mem[nx][ny] {
q = append(q, []int{nx, ny, nPre})
mem[nx][ny] = nPre
}
}
}
}
step++
}
return -1
}