forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1091.go
28 lines (26 loc) · 764 Bytes
/
1091.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
func shortestPathBinaryMatrix(grid [][]int) int {
if len(grid) == 0 {
return -1
}
r, c := len(grid), len(grid[0])
if grid[r-1][c-1] == 1 || grid[0][0] == 1 {
return -1
}
dire := [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
q := [][]int{{0, 0, 1}}
for len(q) > 0 {
pre := q[0]
q = q[1:]
if pre[0] == r-1 && pre[1] == c-1 {
return pre[2]
}
for _, v := range dire {
nx, ny := v[0] + pre[0], v[1] + pre[1]
if nx >= 0 && nx < r && ny >= 0 && ny < c && grid[nx][ny] == 0{
grid[nx][ny] = 1
q = append(q, []int{nx, ny, pre[2] + 1})
}
}
}
return -1
}