Detonate the Maximum Bombs: reachability decides
Problem. Each bomb i sits at (x[i], y[i]) with blast radius r[i].
Detonating it sets off every bomb inside its radius, and those set off theirs,
in a chain reaction. You may start the chain at any single bomb. How many
bombs can you detonate?
bombs = [[0,0,3],[3,0,1],[6,0,1]] → 2
The whole answer, before any explanation: bomb 0’s blast (dashed circle)
reaches bomb 1, so starting at 0 detonates {0, 1} — and nothing reaches
further, so 2 is the maximum.
\begin{tikzpicture}[font=\small,
b/.style={draw,thick,circle,minimum size=0.7cm,font=\small\bfseries},
a/.style={->,very thick,orange}]
\draw[dashed,black!50] (0,0) circle (3);
\draw[dashed,black!50] (3,0) circle (1);
\draw[dashed,black!50] (6,0) circle (1);
\node[b,fill=orange!25] at (0,0) {0};
\node[b,fill=green!30] at (3,0) {1};
\node[b] at (6,0) {2};
\draw[a] (0.5,0.32) -- (2.5,0.32);
\node[font=\scriptsize\ttfamily] at (1.5,0.72) {detonates};
\end{tikzpicture}
The one idea: a detonation chain is graph reachability
Bomb i sets off bomb j exactly when j lies inside i’s blast circle —
squared distances, no square roots:
\[ i \to j \iff (x_i - x_j)^2 + (y_i - y_j)^2 \le r_i^2 \]
That turns the bombs into a directed graph: an edge i → j means “setting
off i eventually sets off j”. The crucial word is directed — bomb 0
reaches bomb 1 above, but bomb 1’s tiny radius reaches nothing back. Starting
at s detonates precisely the vertices reachable from s, so the answer
is the largest reachable set over all n starts.
Why naive simulation explodes
The obvious simulation skips the graph entirely: pick a start, then repeat “any undetonated bomb inside any detonated bomb’s radius goes off” until a full scan changes nothing.
detonated = {s}
changed = True
while changed: # up to n rounds...
changed = False
for i in range(n): # ...each scanning all pairs
for j in detonated:
if dist(i, j) <= r[j]:
...
Each round costs \(O(n^2)\) distance checks and a chain adds as little as one bomb per round, so one start costs \(O(n^3)\) — \(O(n^4)\) over all starts. Worse, a mutual pair with no visited bookkeeping recurses forever: 0 sets off 1 sets off 0 sets off 1… Every later version must remove exactly this cost: build the edges once, and never visit a bomb twice.
First rung: build the graph once
Pay \(O(n^2)\) distance checks up front for an adjacency list, and the repeated rescans disappear. On the running example the six ordered pairs work out to a single edge:
| pair | dist² | \(r^2\) | edge? |
|---|---|---|---|
| 0 → 1 | 9 | 9 | yes (on the circle counts) |
| 1 → 0 | 9 | 1 | no |
| 1 → 2 | 9 | 1 | no |
| 2 → 1 | 9 | 1 | no |
| 0 → 2 | 36 | 9 | no |
| 2 → 0 | 36 | 1 | no |
So adj = [[1], [], []]. The table is the whole graph, computed once and
reused by every search.
The optimal version: DFS from every bomb
From each start, walk its reachable set with an explicit stack and a seen
set — the seen set is what kills both the infinite mutual recursion and the
repeat work:
def maximum_detonation(bombs):
# edge i -> j iff bomb j lies inside bomb i's blast radius;
# the answer is the largest reachable set
n = len(bombs)
adj = [[] for _ in range(n)]
for i in range(n):
x1, y1, r1 = bombs[i]
for j in range(n):
if i == j:
continue
x2, y2, r2 = bombs[j]
if (x1 - x2) ** 2 + (y1 - y2) ** 2 <= r1 * r1:
adj[i].append(j)
best = 0
for s in range(n):
seen = {s}
stack = [s]
count = 0
while stack:
i = stack.pop()
count += 1
for j in adj[i]:
if j not in seen:
seen.add(j)
stack.append(j)
best = max(best, count)
return best
Dry run on [[0,0,3],[3,0,1],[6,0,1]]
Legend, stated once: orange is the bomb detonating right now, green is already detonated, dashed circles are blast radii.
Step 1 — the graph. Bomb 0’s radius-3 circle passes exactly through bomb 1
(distance 3 ≤ 3, edge kept); bomb 1’s radius-1 circle reaches nobody. One
orange arrow, adj = [[1], [], []].
\begin{tikzpicture}[font=\small,
b/.style={draw,thick,circle,minimum size=0.7cm,font=\small\bfseries},
a/.style={->,very thick,orange}]
\draw[dashed,black!50] (0,0) circle (3);
\draw[dashed,black!50] (3,0) circle (1);
\draw[dashed,black!50] (6,0) circle (1);
\node[b] at (0,0) {0};
\node[b] at (3,0) {1};
\node[b] at (6,0) {2};
\draw[a] (0.5,0.32) -- (2.5,0.32);
\node[font=\scriptsize\ttfamily] at (1.5,0.72) {0 $\to$ 1, the only edge};
\end{tikzpicture}
Step 2 — start at 0. stack = [0], seen = {0}. Pop 0 (count = 1),
push 1. Pop 1 (count = 2), no neighbours. seen = {0, 1}, best = 2.
\begin{tikzpicture}[font=\small,
b/.style={draw,thick,circle,minimum size=0.7cm,font=\small\bfseries},
a/.style={->,very thick,orange}]
\draw[dashed,black!50] (0,0) circle (3);
\draw[dashed,black!50] (3,0) circle (1);
\draw[dashed,black!50] (6,0) circle (1);
\node[b,fill=orange!45] at (0,0) {0};
\node[b,fill=green!30] at (3,0) {1};
\node[b] at (6,0) {2};
\draw[a] (0.5,0.32) -- (2.5,0.32);
\node[font=\scriptsize\ttfamily] at (1.5,-3.4) {stack: [0] $\to$ [1] $\to$ [], count = 2};
\end{tikzpicture}
Step 3 — start at 1, then 2. From 1 the stack dies immediately
(count = 1); from 2 likewise (count = 1). best stays 2.
\begin{tikzpicture}[font=\small,
b/.style={draw,thick,circle,minimum size=0.7cm,font=\small\bfseries}]
\draw[dashed,black!50] (0,0) circle (3);
\draw[dashed,black!50] (3,0) circle (1);
\draw[dashed,black!50] (6,0) circle (1);
\node[b] at (0,0) {0};
\node[b,fill=orange!45] at (3,0) {1};
\node[b] at (6,0) {2};
\node[font=\scriptsize\ttfamily] at (3,-3.4) {no outgoing edges, count = 1};
\end{tikzpicture}
The finished artifact, so you can verify without re-reading the diagrams:
adj = [[1], [], []]
count = [2, 1, 1] ← answer is max = 2
The code in Go
func maximumDetonation(bombs [][]int) int {
// edge i -> j iff bomb j lies inside bomb i's blast radius;
// the answer is the largest reachable set
n := len(bombs)
adj := make([][]int, n)
for i := range bombs {
for j := range bombs {
if i == j {
continue
}
dx := int64(bombs[i][0] - bombs[j][0])
dy := int64(bombs[i][1] - bombs[j][1])
r := int64(bombs[i][2])
if dx*dx+dy*dy <= r*r {
adj[i] = append(adj[i], j)
}
}
}
best := 0
for s := 0; s < n; s++ {
seen := make([]bool, n)
seen[s] = true
stack := []int{s}
count := 0
for len(stack) > 0 {
i := stack[len(stack)-1]
stack = stack[:len(stack)-1]
count++
for _, j := range adj[i] {
if !seen[j] {
seen[j] = true
stack = append(stack, j)
}
}
}
if count > best {
best = count
}
}
return best
}
Same adjacency build, same stack discipline, same names — a straight port,
except the squared distances use int64 (see the last edge case).
A fourth way: condense, then bitsets
When the graph is dense, the n separate searches redo each other’s work.
Condense strongly connected components (Tarjan, \(O(n + E)\)) into a DAG —
mutual-detonation clusters collapse to single nodes — then process the DAG in
reverse topological order, where each component’s reachable set is the bitwise
OR of its successors’, stored as a bitset:
reach[u] = (1 << u) | reach[v1] | reach[v2] | ...
Each OR touches \(n/64\) machine words, so the whole closure costs \(O(E \cdot n / 64)\) instead of \(O(n \cdot (n + E))\). The answer is the largest total weight (component sizes, not node counts) over all components. For \(n \le 100\) this is a wash with plain DFS — it only pays off on much larger graphs — which is why the version above is the one to present.
Complexity
| Approach | Time | Extra space |
|---|---|---|
| Fixpoint rescan per start | \(O(n^4)\) | \(O(n)\) |
| Adjacency + DFS per start | \(O(n \cdot (n + E)) = O(n^3)\) | \(O(n + E)\) |
| SCC condensation + bitset DP | \(O(E \cdot n / 64)\) | \(O(n^2 / 64)\) |
With \(n \le 100\) the middle row is at most \(10^6\) operations. Every start’s
search visits each vertex and edge once — the seen set guarantees it.
Edge cases
- A single bomb (
[[0,0,1]]): no edges, every search counts 1 — the loops handle it with no special case. - Nothing in range (
[[1,1,5],[10,10,5]]): \(\text{dist}^2 = 162 > 25\) both ways,adjis empty, answer 1. - Everything in range (all radii huge): one strongly connected component,
every search counts
n— and the bitset version collapses it to one node. - Overflow in squared distances: coordinates and radii reach \(10^5\), so
\(\text{dist}^2\) reaches \(8 \times 10^{10}\) — far past 32-bit
int. Python integers are unbounded, but in Go the subtraction and squares must useint64, as the code above does.
Trying every start and keeping the biggest reachable set is the same shape as Keys and Rooms — except here the rooms have directed doors, and you must try every key.