{"id":9139,"date":"2021-12-12T13:04:14","date_gmt":"2021-12-12T21:04:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9139"},"modified":"2021-12-12T13:05:13","modified_gmt":"2021-12-12T21:05:13","slug":"leetcode-2101-detonate-the-maximum-bombs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-2101-detonate-the-maximum-bombs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2101. Detonate the Maximum Bombs"},"content":{"rendered":"\n<p>You are given a list of bombs. The&nbsp;<strong>range<\/strong>&nbsp;of a bomb is defined as the area where its effect can be felt. This area is in the shape of a&nbsp;<strong>circle<\/strong>&nbsp;with the center as the location of the bomb.<\/p>\n\n\n\n<p>The bombs are represented by a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>bombs<\/code>&nbsp;where&nbsp;<code>bombs[i] = [x<sub>i<\/sub>, y<sub>i<\/sub>, r<sub>i<\/sub>]<\/code>.&nbsp;<code>x<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>y<sub>i<\/sub><\/code>&nbsp;denote the X-coordinate and Y-coordinate of the location of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;bomb, whereas&nbsp;<code>r<sub>i<\/sub><\/code>&nbsp;denotes the&nbsp;<strong>radius<\/strong>&nbsp;of its range.<\/p>\n\n\n\n<p>You may choose to detonate a&nbsp;<strong>single<\/strong>&nbsp;bomb. When a bomb is detonated, it will detonate&nbsp;<strong>all bombs<\/strong>&nbsp;that lie in its range. These bombs will further detonate the bombs that lie in their ranges.<\/p>\n\n\n\n<p>Given the list of&nbsp;<code>bombs<\/code>, return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;number of bombs that can be detonated if you are allowed to detonate&nbsp;<strong>only one<\/strong>&nbsp;bomb<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/11\/06\/desmos-eg-3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> bombs = [[2,1,3],[6,1,4]]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nThe above figure shows the positions and ranges of the 2 bombs.\nIf we detonate the left bomb, the right bomb will not be affected.\nBut if we detonate the right bomb, both bombs will be detonated.\nSo the maximum bombs that can be detonated is max(1, 2) = 2.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/11\/06\/desmos-eg-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> bombs = [[1,1,5],[10,10,5]]\n<strong>Output:<\/strong> 1\n<strong>Explanation:\n<\/strong>Detonating either bomb will not detonate the other bomb, so the maximum number of bombs that can be detonated is 1.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/11\/07\/desmos-eg1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> bombs = [[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong>\nThe best bomb to detonate is bomb 0 because:\n- Bomb 0 detonates bombs 1 and 2. The red circle denotes the range of bomb 0.\n- Bomb 2 detonates bomb 3. The blue circle denotes the range of bomb 2.\n- Bomb 3 detonates bomb 4. The green circle denotes the range of bomb 3.\nThus all 5 bombs are detonated.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= bombs.length&nbsp;&lt;= 100<\/code><\/li><li><code>bombs[i].length == 3<\/code><\/li><li><code>1 &lt;= x<sub>i<\/sub>, y<sub>i<\/sub>, r<sub>i<\/sub>&nbsp;&lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation w\/ BFS<\/strong><\/h2>\n\n\n\n<p>Enumerate the bomb to detonate, and simulate the process using BFS.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>3<\/sup>)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maximumDetonation(vector<vector<int>>& bombs) {\n    const int n = bombs.size();    \n    auto check = [&](int i, int j) -> bool {\n      long x1 = bombs[i][0], y1 = bombs[i][1], r1 = bombs[i][2];\n      long x2 = bombs[j][0], y2 = bombs[j][1], r2 = bombs[j][2];\n      return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) <= r1 * r1);          \n    };\n    int ans = 0;\n    for (int s = 0; s < n; ++s) {\n      int count = 0;\n      queue<int> q{{s}};\n      vector<int> seen(n);\n      seen[s] = 1;\n      while (!q.empty()) {\n        ++count;\n        int i = q.front(); q.pop();\n        for (int j = 0; j < n; ++j)\n          if (check(i, j) &#038;&#038; !seen[j]++)\n            q.push(j);\n      }\n      ans = max(ans, count);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a list of bombs. The&nbsp;range&nbsp;of a bomb is defined as the area where its effect can be felt. This area is in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[34,284,177,179],"class_list":["post-9139","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-bfs","tag-geometry","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9139"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9139\/revisions"}],"predecessor-version":[{"id":9142,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9139\/revisions\/9142"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}