88
88
89
89
### 方法一:BFS
90
90
91
- 枚举每个炸弹 k 作为起始引爆点,BFS 搜索能影响到的所有炸弹的数量,取其最大值。
91
+ 我们定义一个长度为 $n$ 的数组 $g$,其中 $g[ i] $ 表示炸弹 $i$ 的爆炸范围内可以引爆的所有炸弹的下标。
92
+
93
+ 然后,我们遍历所有炸弹,对于两个炸弹 $(x_1, y_1, r_1)$ 和 $(x_2, y_2, r_2)$,我们计算它们之间的距离 $\text{dist} = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$。如果 $\text{dist} \leq r_1$,那么炸弹 $i$ 的爆炸范围内可以引爆炸弹 $j$,我们就将 $j$ 添加到 $g[ i] $ 中。如果 $\text{dist} \leq r_2$,那么炸弹 $j$ 的爆炸范围内可以引爆炸弹 $i$,我们就将 $i$ 添加到 $g[ j] $ 中。
94
+
95
+ 接下来,我们遍历所有炸弹,对于每个炸弹 $k$,我们使用广度优先搜索计算炸弹 $k$ 的爆炸范围内可以引爆的所有炸弹的下标,并记录下来。如果这些炸弹的数量等于 $n$,那么我们就可以引爆所有炸弹,直接返回 $n$。否则,我们记录下来这些炸弹的数量,并返回最大值。
96
+
97
+ 时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为炸弹的数量。
92
98
93
99
<!-- tabs:start -->
94
100
@@ -97,82 +103,77 @@ tags:
97
103
``` python
98
104
class Solution :
99
105
def maximumDetonation (self , bombs : List[List[int ]]) -> int :
100
- def check (i , j ):
101
- if i == j:
102
- return False
103
- x, y = bombs[i][0 ] - bombs[j][0 ], bombs[i][1 ] - bombs[j][1 ]
104
- r = bombs[i][2 ]
105
- return r * r >= x * x + y * y
106
-
107
- g = defaultdict(list )
108
106
n = len (bombs)
109
- for i in range (n):
110
- for j in range (n):
111
- if check(i, j):
107
+ g = [[] for _ in range (n)]
108
+ for i in range (n - 1 ):
109
+ x1, y1, r1 = bombs[i]
110
+ for j in range (i + 1 , n):
111
+ x2, y2, r2 = bombs[j]
112
+ dist = hypot(x1 - x2, y1 - y2)
113
+ if dist <= r1:
112
114
g[i].append(j)
115
+ if dist <= r2:
116
+ g[j].append(i)
113
117
ans = 0
114
118
for k in range (n):
115
- q = deque([k])
116
- vis = [False ] * n
117
- vis[k] = True
118
- cnt = 0
119
- while q:
120
- i = q.popleft()
121
- cnt += 1
119
+ vis = {k}
120
+ q = [k]
121
+ for i in q:
122
122
for j in g[i]:
123
- if not vis[j] :
124
- vis[j] = True
123
+ if j not in vis:
124
+ vis.add(j)
125
125
q.append(j)
126
- ans = max (ans, cnt)
126
+ if len (vis) == n:
127
+ return n
128
+ ans = max (ans, len (vis))
127
129
return ans
128
130
```
129
131
130
132
#### Java
131
133
132
134
``` java
133
135
class Solution {
134
- private int [][] bombs;
135
-
136
136
public int maximumDetonation (int [][] bombs ) {
137
- this . bombs = bombs;
138
137
int n = bombs. length;
139
- boolean [][] g = new boolean [n][n];
140
- for (int i = 0 ; i < n; ++ i) {
141
- for (int j = 0 ; j < n; ++ j) {
142
- g[i][j] = check(i, j);
138
+ List<Integer > [] g = new List [n];
139
+ Arrays . setAll(g, k - > new ArrayList<> ());
140
+ for (int i = 0 ; i < n - 1 ; ++ i) {
141
+ for (int j = i + 1 ; j < n; ++ j) {
142
+ int [] p1 = bombs[i], p2 = bombs[j];
143
+ double dist = Math . hypot(p1[0 ] - p2[0 ], p1[1 ] - p2[1 ]);
144
+ if (dist <= p1[2 ]) {
145
+ g[i]. add(j);
146
+ }
147
+ if (dist <= p2[2 ]) {
148
+ g[j]. add(i);
149
+ }
143
150
}
144
151
}
145
152
int ans = 0 ;
153
+ boolean [] vis = new boolean [n];
146
154
for (int k = 0 ; k < n; ++ k) {
147
- Deque<Integer > q = new ArrayDeque<> ();
148
- q. offer(k);
149
- boolean [] vis = new boolean [n];
155
+ Arrays . fill(vis, false );
150
156
vis[k] = true ;
151
157
int cnt = 0 ;
158
+ Deque<Integer > q = new ArrayDeque<> ();
159
+ q. offer(k);
152
160
while (! q. isEmpty()) {
153
161
int i = q. poll();
154
162
++ cnt;
155
- for (int j = 0 ; j < n; ++ j ) {
156
- if (g[i][j] && ! vis[j]) {
163
+ for (int j : g[i] ) {
164
+ if (! vis[j]) {
157
165
vis[j] = true ;
158
166
q. offer(j);
159
167
}
160
168
}
161
169
}
170
+ if (cnt == n) {
171
+ return n;
172
+ }
162
173
ans = Math . max(ans, cnt);
163
174
}
164
175
return ans;
165
176
}
166
-
167
- private boolean check (int i , int j ) {
168
- if (i == j) {
169
- return false ;
170
- }
171
- long x = bombs[i][0 ] - bombs[j][0 ];
172
- long y = bombs[i][1 ] - bombs[j][1 ];
173
- long r = bombs[i][2 ];
174
- return r * r >= x * x + y * y;
175
- }
176
177
}
177
178
```
178
179
@@ -183,64 +184,67 @@ class Solution {
183
184
public:
184
185
int maximumDetonation(vector<vector<int >>& bombs) {
185
186
int n = bombs.size();
186
- vector<vector<bool >> g(n, vector<bool >(n));
187
- for (int i = 0; i < n; ++i)
188
- for (int j = 0; j < n; ++j)
189
- g[ i] [ j ] = check(i, j, bombs);
187
+ vector<int > g[ n] ;
188
+ for (int i = 0; i < n - 1; ++i) {
189
+ for (int j = i + 1; j < n; ++j) {
190
+ auto& p1 = bombs[ i] ;
191
+ auto& p2 = bombs[ j] ;
192
+ auto dist = hypot(p1[ 0] - p2[ 0] , p1[ 1] - p2[ 1] );
193
+ if (dist <= p1[ 2] ) {
194
+ g[ i] .push_back(j);
195
+ }
196
+ if (dist <= p2[ 2] ) {
197
+ g[ j] .push_back(i);
198
+ }
199
+ }
200
+ }
190
201
int ans = 0;
202
+ bool vis[ n] ;
191
203
for (int k = 0; k < n; ++k) {
192
- queue<int > q{{k}};
193
- vector<bool > vis(n);
204
+ memset(vis, false, sizeof(vis));
205
+ queue<int > q;
206
+ q.push(k);
194
207
vis[ k] = true;
195
208
int cnt = 0;
196
209
while (!q.empty()) {
197
210
int i = q.front();
198
211
q.pop();
199
212
++cnt;
200
- for (int j = 0; j < n; ++j ) {
201
- if (g [ i ] [ j ] && !vis[ j] ) {
213
+ for (int j : g [ i ] ) {
214
+ if (!vis[ j] ) {
202
215
vis[ j] = true;
203
216
q.push(j);
204
217
}
205
218
}
206
219
}
220
+ if (cnt == n) {
221
+ return n;
222
+ }
207
223
ans = max(ans, cnt);
208
224
}
209
225
return ans;
210
226
}
211
-
212
- bool check(int i, int j, vector<vector<int>>& bombs) {
213
- if (i == j) return false;
214
- long long x = bombs[i][0] - bombs[j][0];
215
- long long y = bombs[i][1] - bombs[j][1];
216
- long long r = bombs[i][2];
217
- return r * r >= x * x + y * y;
218
- }
219
227
};
220
228
```
221
229
222
230
#### Go
223
231
224
232
```go
225
- func maximumDetonation (bombs [][]int ) int {
226
- check := func (i, j int ) bool {
227
- if i == j {
228
- return false
229
- }
230
- x , y := bombs[i][0 ]-bombs[j][0 ], bombs[i][1 ]-bombs[j][1 ]
231
- r := bombs[i][2 ]
232
- return r*r >= x*x+y*y
233
- }
233
+ func maximumDetonation(bombs [][]int) (ans int) {
234
234
n := len(bombs)
235
- g := make ([][]bool , n)
236
- for i := range g {
237
- g[i] = make ([]bool , n)
238
- for j := range g[i] {
239
- g[i][j] = check (i, j)
235
+ g := make([][]int, n)
236
+ for i, p1 := range bombs[:n-1] {
237
+ for j := i + 1; j < n; j++ {
238
+ p2 := bombs[j]
239
+ dist := math.Hypot(float64(p1[0]-p2[0]), float64(p1[1]-p2[1]))
240
+ if dist <= float64(p1[2]) {
241
+ g[i] = append(g[i], j)
242
+ }
243
+ if dist <= float64(p2[2]) {
244
+ g[j] = append(g[j], i)
245
+ }
240
246
}
241
247
}
242
-
243
- ans := 0
244
248
for k := 0; k < n; k++ {
245
249
q := []int{k}
246
250
vis := make([]bool, n)
@@ -250,16 +254,19 @@ func maximumDetonation(bombs [][]int) int {
250
254
i := q[0]
251
255
q = q[1:]
252
256
cnt++
253
- for j := 0 ; j < n; j++ {
254
- if g[i][j] && !vis[j] {
257
+ for _, j := range g[i] {
258
+ if !vis[j] {
255
259
vis[j] = true
256
260
q = append(q, j)
257
261
}
258
262
}
259
263
}
264
+ if cnt == n {
265
+ return n
266
+ }
260
267
ans = max(ans, cnt)
261
268
}
262
- return ans
269
+ return
263
270
}
264
271
```
265
272
@@ -268,37 +275,38 @@ func maximumDetonation(bombs [][]int) int {
268
275
``` ts
269
276
function maximumDetonation(bombs : number [][]): number {
270
277
const n = bombs .length ;
271
- const g = new Map <number , number []>(bombs .map ((_ , i ) => [i , []]));
272
-
273
- for (let i = 0 ; i < n - 1 ; i ++ ) {
274
- for (let j = 1 ; j < n ; j ++ ) {
275
- const [x1, y1, r1] = bombs [i ];
278
+ const g: number [][] = Array .from ({ length: n }, () => []);
279
+ for (let i = 0 ; i < n - 1 ; ++ i ) {
280
+ const [x1, y1, r1] = bombs [i ];
281
+ for (let j = i + 1 ; j < n ; ++ j ) {
276
282
const [x2, y2, r2] = bombs [j ];
277
- const distance = Math .hypot (x1 - x2 , y1 - y2 );
278
-
279
- if (distance <= r1 ) g .get (i )! .push (j );
280
- if (distance <= r2 ) g .get (j )! .push (i );
283
+ const d = Math .hypot (x1 - x2 , y1 - y2 );
284
+ if (d <= r1 ) {
285
+ g [i ].push (j );
286
+ }
287
+ if (d <= r2 ) {
288
+ g [j ].push (i );
289
+ }
281
290
}
282
291
}
283
-
284
- let res = 0 ;
285
- for (let i = 0 ; i < n ; i ++ ) {
286
- const seen = new Set <number >([i ]);
287
- const q = [i ];
288
-
292
+ let ans = 0 ;
293
+ for (let k = 0 ; k < n ; ++ k ) {
294
+ const vis: Set <number > = new Set ([k ]);
295
+ const q: number [] = [k ];
289
296
for (const i of q ) {
290
- for (const j of g .get (i ) ?? []) {
291
- if (seen .has (j )) continue ;
292
- seen .add (j );
293
- q .push (j );
297
+ for (const j of g [i ]) {
298
+ if (! vis .has (j )) {
299
+ vis .add (j );
300
+ q .push (j );
301
+ }
294
302
}
295
303
}
296
-
297
- if (seen .size === n ) return n ;
298
- res = Math .max (res , seen .size );
304
+ if (vis .size === n ) {
305
+ return n ;
306
+ }
307
+ ans = Math .max (ans , vis .size );
299
308
}
300
-
301
- return res ;
309
+ return ans ;
302
310
}
303
311
```
304
312
0 commit comments