{"id":4251,"date":"2018-11-04T01:00:23","date_gmt":"2018-11-04T08:00:23","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4251"},"modified":"2018-11-17T09:49:44","modified_gmt":"2018-11-17T17:49:44","slug":"leetcode-934-shortest-bridge","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-934-shortest-bridge\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 934. Shortest Bridge"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 934. Shortest Bridge - \u5237\u9898\u627e\u5de5\u4f5c EP230\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/JU-g1mNUaSE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p><a href=\"https:\/\/leetcode.com\/problems\/shortest-bridge\/description\/\">https:\/\/leetcode.com\/problems\/shortest-bridge\/description\/<\/a><\/p>\n<p>In a given 2D binary array\u00a0<code>A<\/code>, there are two islands.\u00a0 (An island is a 4-directionally connected group of\u00a0<code>1<\/code>s not connected to any other 1s.)<\/p>\n<p>Now, we may change\u00a0<code>0<\/code>s to\u00a0<code>1<\/code>s so as to connect the two islands together to form 1 island.<\/p>\n<p>Return the smallest number of\u00a0<code>0<\/code>s that must be flipped.\u00a0 (It is guaranteed that the answer is at least 1.)<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[[0,1],[1,0]]<\/span>\r\n<strong>Output: <\/strong>1\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">[[0,1,0],[0,0,0],[0,0,1]]<\/span>\r\n<strong>Output: <\/strong>2\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">[[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">1<\/span><\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= A.length =\u00a0A[0].length &lt;= 100<\/code><\/li>\n<li><code>A[i][j] == 0<\/code>\u00a0or\u00a0<code>A[i][j] == 1<\/code><\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4329\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/11\/934-ep234.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/11\/934-ep234.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/11\/934-ep234-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/11\/934-ep234-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<h1><strong>Solution: DFS + BFS<\/strong><\/h1>\n<ol>\n<li>Use DFS to find one island and color all the nodes as 2 (BLUE).<\/li>\n<li>Use BFS to find the shortest path from any nodes with color 2 (BLUE) to any nodes with color 1 (RED).<\/li>\n<\/ol>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(mn)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n  int shortestBridge(vector&lt;vector&lt;int&gt;&gt;&amp; A) {\r\n    queue&lt;pair&lt;int, int&gt;&gt; q;\r\n    bool found = false;\r\n    for (int i = 0; i &lt; A.size() &amp;&amp; !found; ++i)\r\n      for (int j = 0; j &lt; A[0].size() &amp;&amp; !found; ++j)\r\n        if (A[i][j]) {\r\n          dfs(A, j, i, q);\r\n          found = true;\r\n        }\r\n    \r\n    int steps = 0;\r\n    vector&lt;int&gt; dirs{0, 1, 0, -1, 0};\r\n    while (!q.empty()) {      \r\n      size_t size = q.size();\r\n      while (size--) {\r\n        int x = q.front().first;\r\n        int y = q.front().second;\r\n        q.pop();\r\n        for (int i = 0; i &lt; 4; ++i) {\r\n          int tx = x + dirs[i];\r\n          int ty = y + dirs[i + 1];\r\n          if (tx &lt; 0 || ty &lt; 0 || tx &gt;= A[0].size() || ty &gt;= A.size() || A[ty][tx] == 2) continue;          \r\n          if (A[ty][tx] == 1) return steps;\r\n          A[ty][tx] = 2;\r\n          q.emplace(tx, ty);\r\n        }\r\n      }\r\n      ++steps;\r\n    }\r\n    return -1;\r\n  }\r\nprivate:  \r\n  void dfs(vector&lt;vector&lt;int&gt;&gt;&amp; A, int x, int y, queue&lt;pair&lt;int, int&gt;&gt;&amp; q) {\r\n    if (x &lt; 0 || y &lt; 0 || x &gt;= A[0].size() || y &gt;= A.size() || A[y][x] != 1) return;\r\n    A[y][x] = 2;\r\n    q.emplace(x, y);\r\n    dfs(A, x - 1, y, q);\r\n    dfs(A, x, y - 1, q);\r\n    dfs(A, x + 1, y, q);\r\n    dfs(A, x, y + 1, q);\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-827-making-a-large-island\/\">\u82b1\u82b1\u9171 LeetCode 827. Making A Large Island<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-695-max-area-of-island\/\">\u82b1\u82b1\u9171 LeetCode 695. Max Area of Island<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-882-reachable-nodes-in-subdivided-graph\/\">\u82b1\u82b1\u9171 LeetCode 882. Reachable Nodes In Subdivided Graph<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-743-network-delay-time\/\">\u82b1\u82b1\u9171 LeetCode 743. Network Delay Time<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem https:\/\/leetcode.com\/problems\/shortest-bridge\/description\/ In a given 2D binary array\u00a0A, there are two islands.\u00a0 (An island is a 4-directionally connected group of\u00a01s not connected to any other&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[34,33,177,87],"class_list":["post-4251","post","type-post","status-publish","format-standard","hentry","category-graph","tag-bfs","tag-dfs","tag-medium","tag-shortest-path","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4251","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=4251"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4251\/revisions"}],"predecessor-version":[{"id":4330,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4251\/revisions\/4330"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}