2101. 引爆最多的炸弹

引爆最多的炸弹

解法一: 深度优先遍历

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
func maximumDetonation(bombs [][]int) int {
n := len(bombs)
grid := make([][]int, n)
for i, bomb1 := range bombs {
x := bomb1[0]
y := bomb1[1]
r := bomb1[2]
for j, bomb2 := range bombs {
distanceX := x - bomb2[0]
distanceY := y - bomb2[1]
if distanceX*distanceX+distanceY*distanceY <= r*r {
grid[i] = append(grid[i], j)
}
}
}
var dfs func(int, []bool) int
dfs = func(i int, visit []bool) int {
if visit[i] {
return 0
}
ans := 1
visit[i] = true
for _, j := range grid[i] {
ans += dfs(j, visit)
}
return ans
}
ans := 0
for i := 0; i < n; i++ {
visit := make([]bool, n)
ans = max(ans, dfs(i, visit))
}
return ans
}
作者

wuhunyu

发布于

2024-07-22

更新于

2025-01-15

许可协议