Problem Description
Given a list of bombs where each bomb is represented by its (x, y) coordinate and a radius, determine the maximum number of bombs that can be detonated if you start by detonating just one bomb. When a bomb explodes, it detonates all other bombs within its circular range, and these bombs can in turn trigger other bombs, forming a chain reaction.
Key Insights
- Model the bombs as nodes in a directed graph.
- There is a directed edge from bomb i to bomb j if bomb j lies within the explosion radius of bomb i.
- Use DFS or BFS to simulate the chain reaction starting from each bomb.
- Track visited bombs to avoid processing a bomb more than once during a chain reaction.
- The final answer is the maximum number of bombs detonated from any initial bomb.
Space and Time Complexity
Time Complexity: O(n^2) for building the graph plus O(n) per DFS/BFS starting from each bomb, resulting in O(n^3) in the worst-case scenario. Given n ≤ 100, this is acceptable. Space Complexity: O(n^2) for storing the graph edges, and O(n) for the recursion/queue during the DFS/BFS.
Solution
We first build a graph where each bomb is a node. We add a directed edge from bomb i to bomb j if the Euclidean distance between them is within bomb i's explosion radius (using squared distances to avoid floating point operations). Afterwards, for each bomb, we simulate the chain reaction using DFS (or BFS) to count the total number of bombs that would detonate. The maximum count among all bombs is the answer.