-
Notifications
You must be signed in to change notification settings - Fork 481
/
1162.go
34 lines (32 loc) · 897 Bytes
/
1162.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
var d = [4][2]int{{0, 1}, {0, -1}, {-1, 0}, {1, 0}}
func maxDistance(grid [][]int) int {
data := make([][]int, 0)
r, c := len(grid), len(grid[0])
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
if grid[i][j] == 1 {
data = append(data, []int{i, j})
}
}
}
if r*c == len(data) || 0 == len(data) {
return -1
}
res := 0
for len(data) > 0 {
dataLen := len(data)
for k := 0; k < dataLen; k++ {
pre := data[0]
data = data[1:]
for _, it := range d {
x, y := it[0] + pre[0], it[1] + pre[1]
if x >= 0 && x < r && y >= 0 && y < c && grid[x][y] == 0 {
grid[x][y] = 1
data = append(data, []int{x, y})
}
}
}
res++
}
return res - 1
}