leetcode 2101. Detonate the Maximum Bombs (causing the most bombs)

insert image description here

bombs is a two-dimensional array, each bombs[i] = [x, y, r] represents a bomb, (x, y) is the two-dimensional coordinates, r is the radius.
When a bomb is ignited, all bombs within a circle with (x, y) as the center and a radius of r will be ignited, causing a chain reaction.
Choose a bomb to ignite, so that the number of bombs that can be ignited at the end is the largest.

Ideas:

Because it is a chain reaction when the bomb is ignited, 1 ignites 2, 2 ignites 3,
so it is a DFS idea.

But only one bomb can be ignited, then find the ℹth one among the bombs 1 to n, so that dfs (bombs, i ) is the largest.

A visited array is required in dfs to record whether it has been visited.

Only one bomb can be ignited at a time, so each dfs(bombs, i) initially ignited is a brand new process, the visited array must be reset, and the
number of bombs cnt ignited is set to 0.

Inside the dfs:
Only when the distance between two bombs is <= the radius of the current bomb, will the next one be ignited and enter the next dfs.
Every time a dfs is entered, it means that the bomb is ignited, cnt++.
In a dfs process, Visited does not need to be set to false after lighting, because only one bomb can be clicked, and once it is clicked, it will disappear.

For example,
suppose 1 can ignite 2, 3, and 2, 3 can ignite 4, and the distance is within the radius.

Then dfs(1) enters dfs(2), dfs(2) enters dfs(4), every time dfs is cnt++, after this round cnt=3, then visited[1,2,4
] = true
and then enter dfs(3) inside dfs(1), find that visited[4] is true inside dfs(3), no longer ignite 4, only ignite 3 itself, this round is cnt++ at dfs(3)
.
Although 3 did not ignite 4, but 4 has been ignited by 2, the total number of 2, 3, and 4 bombs ignited by 1 is consistent.
So dfs(1) final cnt=4.

Then start a new round of dfs(2). dfs(3), dfs(4)

To do a pruning, when a certain dfs result is n (total number of bombs), there is no need to calculate again, because there are no more.

In addition, the maximum value of x, y, r is 10 5 , which means that the range of integers will be exceeded when squaring, so the long type is used.

class Solution {
    
       
    int n;
    int count = 0;
    public int maximumDetonation(int[][] bombs) {
    
    
        n = bombs.length;
        int res = 0;

        for(int i = 0; i < n; i++) {
    
               
            count = 0;
            
            dfs(bombs,i, new boolean[n]);       
            res = Math.max(res, count);
            if(res == n) return res;
        }
        return res;
    }

    void dfs(int[][] bombs, int idx, boolean[] visited) {
    
           
        count ++;
        visited[idx] = true;
        
        for(int i = 0; i < n; i++) {
    
    
            if(visited[i]) continue;
            
            long dx = bombs[i][0] - bombs[idx][0];
            long dy = bombs[i][1] - bombs[idx][1];
            long r = bombs[idx][2];
            
            if(dx*dx + dy*dy <= r*r) dfs(bombs, i, visited);
        }     
    }  
}

Guess you like

Origin blog.csdn.net/level_code/article/details/131002158