---
tags: leetcode
---
# [2101. Detonate the Maximum Bombs](https://leetcode.com/problems/detonate-the-maximum-bombs/)
---
# My Solution
## Solution 1: DFS
### The Key Idea for Solving This Coding Question
Count the number of nodes in the largest component.
### C++ Code
```cpp=
#define WHITE (1)
#define GRAY (2)
#define BLACK (3)
class Solution {
public:
int maximumDetonation(vector<vector<int>> &bombs) {
int numBombs = bombs.size();
vector<vector<int>> graph(numBombs);
for (int i = 0; i + 1 < numBombs; ++i) {
unsigned long xi = bombs[i][0], yi = bombs[i][1], ri = bombs[i][2];
for (int j = i + 1; j < numBombs; ++j) {
unsigned long xj = bombs[j][0], yj = bombs[j][1], rj = bombs[j][2];
unsigned long distance = (xi - xj) * (xi - xj) + (yi - yj) * (yi - yj);
if (distance <= ri * ri) {
graph[i].push_back(j);
}
if (distance <= rj * rj) {
graph[j].push_back(i);
}
}
}
int maxBombs = 0;
for (int i = 0; i < numBombs; ++i) {
vector<int> state(numBombs, WHITE);
maxBombs = max(maxBombs, dfs(graph, state, i));
}
return maxBombs;
}
private:
int dfs(vector<vector<int>> &graph, vector<int> &state, int currBomb) {
state[currBomb] = GRAY;
int cnt = 1;
for (auto &nextBomb : graph[currBomb]) {
if (state[nextBomb] != WHITE) {
continue;
}
cnt += dfs(graph, state, nextBomb);
}
state[currBomb] = BLACK;
return cnt;
}
};
```
### Time Complexity
$O(V+E)$
$V$ is the length of `bombs`.
$E$ is the number of edges in the graph.
### Space Complexity
$O(V+E)$
## Solution 2: BFS
### The Key Idea for Solving This Coding Question
Count the number of nodes in the largest component.
### C++ Code
```cpp=
#define WHITE (0)
#define GRAY (1)
#define BLACK (2)
class Solution {
public:
int maximumDetonation(vector<vector<int>> &bombs) {
int numBombs = bombs.size();
vector<vector<int>> graph(numBombs);
for (int i = 0; i + 1 < bombs.size(); ++i) {
for (int j = i + 1; j < bombs.size(); ++j) {
unsigned long xi = bombs[i][0], yi = bombs[i][1], ri = bombs[i][2];
unsigned long xj = bombs[j][0], yj = bombs[j][1], rj = bombs[j][2];
unsigned long distance = (xi - xj) * (xi - xj) + (yi - yj) * (yi - yj);
if (distance <= ri * ri) {
graph[i].push_back(j);
}
if (distance <= rj * rj) {
graph[j].push_back(i);
}
}
}
int maxBombs = 0;
for (int i = 0; i < numBombs; ++i) {
vector<int> state(numBombs, WHITE);
maxBombs = max(maxBombs, bfs(graph, state, i));
}
return maxBombs;
}
private:
int bfs(vector<vector<int>> &graph, vector<int> &state, int firstBomb) {
queue<int> q;
int cnt = 1;
q.push(firstBomb);
state[firstBomb] = GRAY;
while (!q.empty()) {
int currBomb = q.front();
q.pop();
for (auto &nextBomb : graph[currBomb]) {
if (state[nextBomb] != WHITE) {
continue;
}
q.push(nextBomb);
state[nextBomb] = GRAY;
++cnt;
}
state[currBomb] = BLACK;
}
return cnt;
}
};
```
### Time Complexity
$O(V+E)$
$V$ is the length of `bombs`.
$E$ is the number of edges in the graph.
### Space Complexity
$O(V+E)$
# Miscellaneous
[695. Max Area of Island](https://leetcode.com/problems/max-area-of-island/)
<!--
# Test Cases
```
[[2,1,3],[6,1,4]]
```
```
[[1,1,5],[10,10,5]]
```
```
[[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]]
```
* Runtime Error
```
[[1,1,100000],[100000,100000,1]]
```
* Wrong Answer
```
[[85024,58997,3532],[65196,42043,9739],[85872,75029,3117],[73014,91183,7092],[29098,40864,7624],[11469,13607,4315],[98722,69681,9656],[75140,42250,421],[92580,44040,4779],[58474,78273,1047],[27683,4203,6186],[10714,24238,6243],[60138,81791,3496],[16227,92418,5622],[60496,64917,2463],[59241,62074,885],[11961,163,5815],[37757,43214,3402],[21094,98519,1678],[49368,22385,1431],[6343,53798,159],[80129,9282,5139],[69565,32036,6827],[59372,64978,6575],[44948,71199,7095],[46390,91701,1667],[37144,98691,8128],[13558,81505,4653],[41234,48161,9304],[14852,3206,5369]]
```
-->