https://leetcode.cn/problems/detonate-the-maximum-bombs/description/
func maximumDetonation(bombs [][]int) int {
n := len(bombs)
g := make([][]int, n)
for i := range g {
g[i] = make([]int, n)
for j := range g[i] {
g[i][j] = 0
}
}
for i := 0; i < n - 1; i++ {
for j := i + 1; j < n; j++ {
atob, btoa := adjacent(bombs[i], bombs[j])
if atob {
g[i][j] = 1
}
if btoa {
g[j][i] = 1
}
}
}
ans := 1
queue := make([]int, 0)
for i := 0; i < n; i++ {
vis := make([]bool, n)
vis[i] = true
queue = append(queue, i)
cnt := 1
for len(queue) > 0 {
node := queue[0]
queue = queue[1:]
for j, val := range g[node] {
if val == 1 && !vis[j] {
cnt++
vis[j] = true
queue = append(queue, j)
}
}
}
ans = max(cnt, ans)
}
return ans
}
func adjacent(bomb1, bomb2 []int) (bool, bool) {
x1, y1, r1 := bomb1[0], bomb1[1], bomb1[2]
x2, y2, r2 := bomb2[0], bomb2[1], bomb2[2]
dis := (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1)
atob, btoa := false, false
if r1 * r1 >= dis {
atob = true
}
if r2 * r2 >= dis {
btoa = true
}
return atob, btoa
}
在有向图的bfs中,注意将vis数组设置在for循环中。在无向图中,vis定义在外面。(?