forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortest-bridge.go
executable file
·73 lines (62 loc) · 1.43 KB
/
shortest-bridge.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package problem0934
var dx = []int{1, -1, 0, 0}
var dy = []int{0, 0, 1, -1}
func shortestBridge(A [][]int) int {
m, n := len(A), len(A[0])
p, q := searchForIsland(A)
isChecked := [100][100]bool{}
isChecked[p][q] = true
island := make([][]int, 0, 1024)
island = append(island, []int{p, q})
// bfs 查找岛中全部的点
for idx := 0; idx < len(island); idx++ {
i, j := island[idx][0], island[idx][1]
for l := 0; l < 4; l++ {
x, y := i+dx[l], j+dy[l]
if 0 <= x && x < m &&
0 <= y && y < n &&
A[x][y] == 1 &&
!isChecked[x][y] {
island = append(island, []int{x, y})
isChecked[x][y] = true
}
}
}
bridge := island
length := 0
// 此时, bridge 中所有的点,在 isChecked 中都被标记为 true
// bfs 查找最短的桥
for len(bridge) > 0 {
size := len(bridge)
for idx := 0; idx < size; idx++ {
land := bridge[idx]
i, j := land[0], land[1]
for l := 0; l < 4; l++ {
x, y := i+dx[l], j+dy[l]
if 0 <= x && x < m &&
0 <= y && y < n &&
!isChecked[x][y] {
if A[x][y] == 1 {
return length
}
A[x][y] = 1
bridge = append(bridge, []int{x, y})
isChecked[x][y] = true
}
}
}
bridge = bridge[size:]
length++
}
panic("A has only one island")
}
func searchForIsland(A [][]int) (int, int) {
for i := 0; i < len(A); i++ {
for j := 0; j < len(A[0]); j++ {
if A[i][j] == 1 {
return i, j
}
}
}
panic("A has NO island.")
}