forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
46 lines (45 loc) · 1.06 KB
/
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
37
38
39
40
41
42
43
44
45
46
import "math"
func findShortestWay(maze [][]int, ball []int, hole []int) string {
m, n := len(maze), len(maze[0])
r, c := ball[0], ball[1]
rh, ch := hole[0], hole[1]
q := [][]int{[]int{r, c}}
dist := make([][]int, m)
path := make([][]string, m)
for i := range dist {
dist[i] = make([]int, n)
path[i] = make([]string, n)
for j := range dist[i] {
dist[i][j] = math.MaxInt32
path[i][j] = ""
}
}
dist[r][c] = 0
dirs := map[string][]int{"u": {-1, 0}, "d": {1, 0}, "l": {0, -1}, "r": {0, 1}}
for len(q) > 0 {
p := q[0]
q = q[1:]
i, j := p[0], p[1]
for d, dir := range dirs {
a, b := dir[0], dir[1]
x, y := i, j
step := dist[i][j]
for x+a >= 0 && x+a < m && y+b >= 0 && y+b < n && maze[x+a][y+b] == 0 && (x != rh || y != ch) {
x += a
y += b
step++
}
if dist[x][y] > step || (dist[x][y] == step && (path[i][j]+d) < path[x][y]) {
dist[x][y] = step
path[x][y] = path[i][j] + d
if x != rh || y != ch {
q = append(q, []int{x, y})
}
}
}
}
if path[rh][ch] == "" {
return "impossible"
}
return path[rh][ch]
}