Detonate the Maximum Bombs | Optimized Solution | Daily Leetcode Challenge
\nclass Solution {\npublic:\n    // Helper function to calculate distance between two points\n    int distance(int x, int y, int x1, int y1)\n    {\n        dou
```\nclass Solution {\npublic:\n    // Helper function to calculate distance between two points\n    int distance(int x, int y, int x1, int y1)\n    {\n        double temp = sqrt(pow(x1 - x, 2) + pow(y1 - y, 2));  // Euclidean distance formula\n        return ceil(temp);  // Round up the distance to the nearest integer\n    }\n    \n    // Depth-first search to calculate the number of bombs that can be detonated starting from a given index\n    int dfs(int index, vector<int>& visited, vector<vector<int>>& graph)\n    {\n        int temp = 1;  // Initialize the count to 1, considering the current bomb itself as detonated\n        visited[index] = 1;  // Mark the current index as visited\n        \n        // Iterate through the adjacent bombs in the graph\n        for (auto x : graph[index])\n        {\n            if (visited[x] == 0)  // If the adjacent bomb has not been visited\n            {\n                temp += dfs(x, visited, graph);  // Recursively calculate the count of detonated bombs\n            }\n        }\n        \n        return temp;  // Return the total count of detonated bombs\n    }\n    \n    int maximumDetonation(vector<vector<int>>& bombs) {\n        int n = bombs.size();  // Total number of bombs\n        \n        // Create a graph to represent the relationships between bombs\n        vector<vector<int>> graph(n);\n        \n        // Construct the graph by checking the distances between each pair of bombs\n        for (int i = 0; i < n; i++)\n        {\n            for (int j = i + 1; j < n; j++)\n            {\n                int x = bombs[i][0];\n                int y = bombs[i][1];\n                int x1 = bombs[j][0];\n                int y1 = bombs[j][1];\n                int r = bombs[i][2];\n                int r1 = bombs[j][2];\n                \n                // Calculate the distance between the bombs and check if they can detonate each other\n                int temp = distance(x, y, x1, y1);\n                if (temp <= r)\n                {\n                    graph[i].push_back(j);  // Add j as an adjacent bomb to i\n                }\n                if (temp <= r1)\n                {\n                    graph[j].push_back(i);  // Add i as an adjacent bomb to j\n                }\n            }\n        }\n        \n        // Perform depth-first search on each bomb to find the maximum number of detonated bombs\n        vector<int> visited;  // Keep track of visited bombs\n        vector<int> x(n, 0);  // Initialize the visited array with zeros\n        int ans = 1;  // Initialize the maximum count of detonated bombs to 1\n        \n        // Iterate through each bomb as a starting point for the depth-first search\n        for (int i = 0; i < n; i++)\n        {\n            visited = x;  // Reset the visited array for each iteration\n            int temp = dfs(i, visited, graph);  // Perform depth-first search and get the count\n            ans = max(ans, temp);  // Update the maximum count if necessary\n        }\n        \n        return ans;  // Return the maximum count of detonated bombs\n    }\n};\n```\n\n**PLEASE UPVOTE :)**
['Depth-First Search', 'C']