Skip to content

Commit 785bbf1

Browse files
committed
feat: add solutions to lc problem: No.2101
No.2101.Detonate the Maximum Bombs
1 parent 67fd98d commit 785bbf1

File tree

6 files changed

+509
-1
lines changed

6 files changed

+509
-1
lines changed

solution/2100-2199/2101.Detonate the Maximum Bombs/README.md

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,196 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
**方法一:BFS**
71+
72+
枚举每个炸弹 k 作为起始引爆点,BFS 搜索能影响到的所有炸弹的数量,取其最大值。
73+
7074
<!-- tabs:start -->
7175

7276
### **Python3**
7377

7478
<!-- 这里可写当前语言的特殊实现逻辑 -->
7579

7680
```python
77-
81+
class Solution:
82+
def maximumDetonation(self, bombs: List[List[int]]) -> int:
83+
def check(i, j):
84+
if i == j:
85+
return False
86+
x, y = bombs[i][0] - bombs[j][0], bombs[i][1] - bombs[j][1]
87+
r = bombs[i][2]
88+
return r * r >= x * x + y * y
89+
90+
g = defaultdict(list)
91+
n = len(bombs)
92+
for i in range(n):
93+
for j in range(n):
94+
if check(i, j):
95+
g[i].append(j)
96+
ans = 0
97+
for k in range(n):
98+
q = deque([k])
99+
vis = [False] * n
100+
vis[k] = True
101+
cnt = 0
102+
while q:
103+
i = q.popleft()
104+
cnt += 1
105+
for j in g[i]:
106+
if not vis[j]:
107+
vis[j] = True
108+
q.append(j)
109+
ans = max(ans, cnt)
110+
return ans
78111
```
79112

80113
### **Java**
81114

82115
<!-- 这里可写当前语言的特殊实现逻辑 -->
83116

84117
```java
118+
class Solution {
119+
private int[][] bombs;
120+
121+
public int maximumDetonation(int[][] bombs) {
122+
this.bombs = bombs;
123+
int n = bombs.length;
124+
boolean[][] g = new boolean[n][n];
125+
for (int i = 0; i < n; ++i) {
126+
for (int j = 0; j < n; ++j) {
127+
g[i][j] = check(i, j);
128+
}
129+
}
130+
int ans = 0;
131+
for (int k = 0; k < n; ++k) {
132+
Deque<Integer> q = new ArrayDeque<>();
133+
q.offer(k);
134+
boolean[] vis = new boolean[n];
135+
vis[k] = true;
136+
int cnt = 0;
137+
while (!q.isEmpty()) {
138+
int i = q.poll();
139+
++cnt;
140+
for (int j = 0; j < n; ++j) {
141+
if (g[i][j] && !vis[j]) {
142+
vis[j] = true;
143+
q.offer(j);
144+
}
145+
}
146+
}
147+
ans = Math.max(ans, cnt);
148+
}
149+
return ans;
150+
}
151+
152+
private boolean check(int i, int j) {
153+
if (i == j) {
154+
return false;
155+
}
156+
long x = bombs[i][0] - bombs[j][0];
157+
long y = bombs[i][1] - bombs[j][1];
158+
long r = bombs[i][2];
159+
return r * r >= x * x + y * y;
160+
}
161+
}
162+
```
163+
164+
### **C++**
165+
166+
```cpp
167+
class Solution {
168+
public:
169+
int maximumDetonation(vector<vector<int>>& bombs) {
170+
int n = bombs.size();
171+
vector<vector<bool>> g(n, vector<bool>(n));
172+
for (int i = 0; i < n; ++i)
173+
for (int j = 0; j < n; ++j)
174+
g[i][j] = check(i, j, bombs);
175+
int ans = 0;
176+
for (int k = 0; k < n; ++k)
177+
{
178+
queue<int> q{{k}};
179+
vector<bool> vis(n);
180+
vis[k] = true;
181+
int cnt = 0;
182+
while (!q.empty())
183+
{
184+
int i = q.front();
185+
q.pop();
186+
++cnt;
187+
for (int j = 0; j < n; ++j)
188+
{
189+
if (g[i][j] && !vis[j])
190+
{
191+
vis[j] = true;
192+
q.push(j);
193+
}
194+
}
195+
}
196+
ans = max(ans, cnt);
197+
}
198+
return ans;
199+
}
200+
201+
bool check(int i, int j, vector<vector<int>>& bombs) {
202+
if (i == j) return false;
203+
long long x = bombs[i][0] - bombs[j][0];
204+
long long y = bombs[i][1] - bombs[j][1];
205+
long long r = bombs[i][2];
206+
return r * r >= x * x + y * y;
207+
}
208+
};
209+
```
85210

211+
### **Go**
212+
213+
```go
214+
func maximumDetonation(bombs [][]int) int {
215+
check := func(i, j int) bool {
216+
if i == j {
217+
return false
218+
}
219+
x, y := bombs[i][0]-bombs[j][0], bombs[i][1]-bombs[j][1]
220+
r := bombs[i][2]
221+
return r*r >= x*x+y*y
222+
}
223+
n := len(bombs)
224+
g := make([][]bool, n)
225+
for i := range g {
226+
g[i] = make([]bool, n)
227+
for j := range g[i] {
228+
g[i][j] = check(i, j)
229+
}
230+
}
231+
232+
ans := 0
233+
for k := 0; k < n; k++ {
234+
q := []int{k}
235+
vis := make([]bool, n)
236+
vis[k] = true
237+
cnt := 0
238+
for len(q) > 0 {
239+
i := q[0]
240+
q = q[1:]
241+
cnt++
242+
for j := 0; j < n; j++ {
243+
if g[i][j] && !vis[j] {
244+
vis[j] = true
245+
q = append(q, j)
246+
}
247+
}
248+
}
249+
ans = max(ans, cnt)
250+
}
251+
return ans
252+
}
253+
254+
func max(a, b int) int {
255+
if a > b {
256+
return a
257+
}
258+
return b
259+
}
86260
```
87261

88262
### **TypeScript**

solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,190 @@ Thus all 5 bombs are detonated.
5858

5959
## Solutions
6060

61+
BFS.
62+
6163
<!-- tabs:start -->
6264

6365
### **Python3**
6466

6567
```python
68+
class Solution:
69+
def maximumDetonation(self, bombs: List[List[int]]) -> int:
70+
def check(i, j):
71+
if i == j:
72+
return False
73+
x, y = bombs[i][0] - bombs[j][0], bombs[i][1] - bombs[j][1]
74+
r = bombs[i][2]
75+
return r * r >= x * x + y * y
6676

77+
g = defaultdict(list)
78+
n = len(bombs)
79+
for i in range(n):
80+
for j in range(n):
81+
if check(i, j):
82+
g[i].append(j)
83+
ans = 0
84+
for k in range(n):
85+
q = deque([k])
86+
vis = [False] * n
87+
vis[k] = True
88+
cnt = 0
89+
while q:
90+
i = q.popleft()
91+
cnt += 1
92+
for j in g[i]:
93+
if not vis[j]:
94+
vis[j] = True
95+
q.append(j)
96+
ans = max(ans, cnt)
97+
return ans
6798
```
6899

69100
### **Java**
70101

71102
```java
103+
class Solution {
104+
private int[][] bombs;
105+
106+
public int maximumDetonation(int[][] bombs) {
107+
this.bombs = bombs;
108+
int n = bombs.length;
109+
boolean[][] g = new boolean[n][n];
110+
for (int i = 0; i < n; ++i) {
111+
for (int j = 0; j < n; ++j) {
112+
g[i][j] = check(i, j);
113+
}
114+
}
115+
int ans = 0;
116+
for (int k = 0; k < n; ++k) {
117+
Deque<Integer> q = new ArrayDeque<>();
118+
q.offer(k);
119+
boolean[] vis = new boolean[n];
120+
vis[k] = true;
121+
int cnt = 0;
122+
while (!q.isEmpty()) {
123+
int i = q.poll();
124+
++cnt;
125+
for (int j = 0; j < n; ++j) {
126+
if (g[i][j] && !vis[j]) {
127+
vis[j] = true;
128+
q.offer(j);
129+
}
130+
}
131+
}
132+
ans = Math.max(ans, cnt);
133+
}
134+
return ans;
135+
}
136+
137+
private boolean check(int i, int j) {
138+
if (i == j) {
139+
return false;
140+
}
141+
long x = bombs[i][0] - bombs[j][0];
142+
long y = bombs[i][1] - bombs[j][1];
143+
long r = bombs[i][2];
144+
return r * r >= x * x + y * y;
145+
}
146+
}
147+
```
148+
149+
### **C++**
150+
151+
```cpp
152+
class Solution {
153+
public:
154+
int maximumDetonation(vector<vector<int>>& bombs) {
155+
int n = bombs.size();
156+
vector<vector<bool>> g(n, vector<bool>(n));
157+
for (int i = 0; i < n; ++i)
158+
for (int j = 0; j < n; ++j)
159+
g[i][j] = check(i, j, bombs);
160+
int ans = 0;
161+
for (int k = 0; k < n; ++k)
162+
{
163+
queue<int> q{{k}};
164+
vector<bool> vis(n);
165+
vis[k] = true;
166+
int cnt = 0;
167+
while (!q.empty())
168+
{
169+
int i = q.front();
170+
q.pop();
171+
++cnt;
172+
for (int j = 0; j < n; ++j)
173+
{
174+
if (g[i][j] && !vis[j])
175+
{
176+
vis[j] = true;
177+
q.push(j);
178+
}
179+
}
180+
}
181+
ans = max(ans, cnt);
182+
}
183+
return ans;
184+
}
185+
186+
bool check(int i, int j, vector<vector<int>>& bombs) {
187+
if (i == j) return false;
188+
long long x = bombs[i][0] - bombs[j][0];
189+
long long y = bombs[i][1] - bombs[j][1];
190+
long long r = bombs[i][2];
191+
return r * r >= x * x + y * y;
192+
}
193+
};
194+
```
195+
196+
### **Go**
197+
198+
```go
199+
func maximumDetonation(bombs [][]int) int {
200+
check := func(i, j int) bool {
201+
if i == j {
202+
return false
203+
}
204+
x, y := bombs[i][0]-bombs[j][0], bombs[i][1]-bombs[j][1]
205+
r := bombs[i][2]
206+
return r*r >= x*x+y*y
207+
}
208+
n := len(bombs)
209+
g := make([][]bool, n)
210+
for i := range g {
211+
g[i] = make([]bool, n)
212+
for j := range g[i] {
213+
g[i][j] = check(i, j)
214+
}
215+
}
216+
217+
ans := 0
218+
for k := 0; k < n; k++ {
219+
q := []int{k}
220+
vis := make([]bool, n)
221+
vis[k] = true
222+
cnt := 0
223+
for len(q) > 0 {
224+
i := q[0]
225+
q = q[1:]
226+
cnt++
227+
for j := 0; j < n; j++ {
228+
if g[i][j] && !vis[j] {
229+
vis[j] = true
230+
q = append(q, j)
231+
}
232+
}
233+
}
234+
ans = max(ans, cnt)
235+
}
236+
return ans
237+
}
72238

239+
func max(a, b int) int {
240+
if a > b {
241+
return a
242+
}
243+
return b
244+
}
73245
```
74246

75247
### **TypeScript**

0 commit comments

Comments
 (0)