Leetcode-2101

Detonate the Maximum Bombs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>

class Solution {
    bool det(std::vector<std::vector<int>>& bombs, int a, int b) {
        long long x = bombs[a][0] - bombs[b][0];
        long long y = bombs[a][1] - bombs[b][1];
        long long r = bombs[a][2];
        return x * x + y * y <= r * r;
    }
        
public:
    int maximumDetonation(std::vector<std::vector<int>>& bombs) {
        int n = bombs.size();
        std::vector<std::vector<bool>> dist(n, std::vector<bool>(n, false));
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (i != j) {
                    dist[i][j] = det(bombs, i, j);
                }
            }
        }
        
        for (int k = 0; k < n; ++k) {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (i != j && dist[i][k] && dist[k][j]) {
                        dist[i][j] = true;
                    }
                }
            }
        }
        
        int res = 0;
        for (int i = 0; i < n; ++i) {
            int cnt = 0;
            for (int j = 0; j < n; ++j) {
                cnt += dist[i][j];
            }
            res = std::max(res, cnt);
        }
        return res + 1;
    }
};

Leetcode-2101