Largest Chain Reaction From a Single Detonation
Trees & GraphsAuto-graded · 20 test casesLast updated
Model one-way blast reach as a directed graph and find the largest set reachable from any single starting node, using exact integer distance checks.
Largest Chain Reaction From a Single Detonation
You are given a collection of explosive devices placed on a flat 2D grid. Each device is described by three integers [x, y, r]: its center is at (x, y) and its blast reaches every point within distance r of that center.
When a device goes off, it sets off every other device whose center lies within or exactly on the edge of its blast circle. In other words, device i sets off device j when the distance between their centers is at most r_i. Devices that are set off explode in turn, and may set off further devices, and so on until nothing new explodes.
You get to choose exactly one device to detonate by hand. Return the largest possible total number of devices that explode (including the one you chose) over all choices of starting device.
Details
- The relation is one-directional. Device
ireaching devicejsays nothing about whetherjreachesi, because only the radius of the exploding device matters. - Only the radius of the device that is exploding is used when deciding whom it sets off.
- Devices may share the same coordinates. They are different devices, and each one can set off the other if the distance (0) is within the exploding device's radius.
- A radius of
0means the device only sets off devices located at exactly the same coordinates. - If the list is empty, return
0. - Compare distances exactly, using integer arithmetic on squared values, to avoid floating-point error.
Input
bombs: a list of[x, y, r]integer triples.
Output
- An integer: the maximum number of devices that can explode from a single manual detonation.
Constraints
0 <= len(bombs) <= 100- Each entry is a list of three integers
[x, y, r] r >= 0- Coordinates and radii are integers whose squares fit comfortably in standard integer arithmetic (up to about
10^5in magnitude)
Examples
Example 1
Input: max_chain_explosion([[0, 0, 2], [2, 0, 2], [5, 0, 1]])
Output: 2
Example 2
Input: max_chain_explosion([[0, 0, 10], [10, 0, 1]])
Output: 2
Hints
Hint 1
Think about who can trigger whom: if a big device covers a small one, does the small one necessarily cover the big one? Choose a graph representation that respects this.
Hint 2
Compare squared distance dx*dx + dy*dy against r*r using integers, so no square roots or floats are needed.
Hint 3
For each possible starting device, run a DFS or BFS over the directed graph and count visited devices; the answer is the maximum count.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Treat each device as a node in a directed graph. Add an edge i -> j whenever device j is inside or on the boundary of device i's blast circle, which is the integer test
(xi - xj)^2 + (yi - yj)^2 <= ri^2
Detonating a device s makes explode exactly the set of devices reachable from s along these edges. So the answer is the maximum, over all start nodes, of the size of the reachable set.
Build the adjacency lists with an O(n^2) double loop, then run a DFS (iterative, with a stack) from every node, counting visited nodes, and keep the best count.
Why it is correct
- A device explodes if and only if some already-exploding device has it within range. That is precisely graph reachability from the starting node, and DFS finds the full reachable set.
- Edges must be directed. A large device can reach a small one without the reverse being true, so connected components or union-find would give wrong answers.
- Comparing squared integer distances with
r^2is exact, so there are no floating-point issues, and the "at most" (boundary included) rule is respected. - A device always counts itself, so a single device gives
1, and an empty list gives0because the loop never runs. - Zero radius and duplicate positions work naturally: distance 0 satisfies
0 <= r^2, so co-located devices set each other off.
Complexity
- Graph construction: O(n^2) time.
- Searches: one DFS per start node, each O(n + E) with E up to n^2, so O(n^3) worst case overall (about 10^6 operations for n = 100).
- Space: O(n^2) for adjacency lists, plus O(n) per search.
Reference solution
from typing import List
def max_chain_explosion(bombs: List[List[int]]) -> int:
n = len(bombs)
graph = [[] for _ in range(n)]
for i, (x1, y1, radius) in enumerate(bombs):
radius_squared = radius * radius
for j, (x2, y2, _) in enumerate(bombs):
if i != j and (x1 - x2) ** 2 + (y1 - y2) ** 2 <= radius_squared:
graph[i].append(j)
best = 0
for start in range(n):
seen = {start}
stack = [start]
while stack:
current = stack.pop()
for neighbor in graph[current]:
if neighbor not in seen:
seen.add(neighbor)
stack.append(neighbor)
best = max(best, len(seen))
return best