{"id":5817,"date":"2019-11-09T22:38:30","date_gmt":"2019-11-10T06:38:30","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5817"},"modified":"2019-11-13T02:07:19","modified_gmt":"2019-11-13T10:07:19","slug":"leetcode-1254-number-of-closed-islands","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1254-number-of-closed-islands\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1254. Number of Closed Islands"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1252 1253 1254 1255 Weekly Contest 162  - \u5237\u9898\u627e\u5de5\u4f5c\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/1XMpzhFUvco?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>\n<\/div><\/figure>\n\n\n\n<p>Given a 2D&nbsp;<code>grid<\/code>&nbsp;consists of&nbsp;<code>0s<\/code>&nbsp;(land)&nbsp;and&nbsp;<code>1s<\/code>&nbsp;(water).&nbsp; An&nbsp;<em>island<\/em>&nbsp;is a maximal 4-directionally connected group of&nbsp;<code>0s<\/code>&nbsp;and a&nbsp;<em>closed island<\/em>&nbsp;is an island&nbsp;<strong>totally<\/strong>&nbsp;(all left, top, right, bottom) surrounded by&nbsp;<code>1s.<\/code><\/p>\n\n\n\n<p>Return the number of&nbsp;<em>closed islands<\/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\/2019\/10\/31\/sample_3_1610.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> \nIslands in gray are closed because they are completely surrounded by water (group of 1s).<\/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\/2019\/10\/31\/sample_4_1610.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,1,1,1,1,1,1],\n&nbsp;              [1,0,0,0,0,0,1],\n&nbsp;              [1,0,1,1,1,0,1],\n&nbsp;              [1,0,1,0,1,0,1],\n&nbsp;              [1,0,1,1,1,0,1],\n&nbsp;              [1,0,0,0,0,0,1],\n               [1,1,1,1,1,1,1]]\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= grid.length, grid[0].length &lt;= 100<\/code><\/li><li><code>0 &lt;= grid[i][j] &lt;=1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS\/Backtracking<\/strong><\/h2>\n\n\n\n<p>For each connected component, if it can reach the boundary then it&#8217;s not a closed island.<\/p>\n\n\n\n<p>Time complexity: O(n*m)<br>Space complexity: O(n*m)<\/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 closedIsland(vector<vector<int>>& grid) {    \n    const int n = grid.size();\n    const int m = grid[0].size();    \n    function<int(int, int)> dfs = [&](int x, int y) {\n      if (x < 0 || y < 0 || x >= m || y >= n) return 0;      \n      if (grid[y][x]++) return 1;\n      return dfs(x + 1, y) & dfs(x - 1, y) & dfs(x, y + 1) & dfs(x, y - 1);\n    };\n    \n    int ans = 0;\n    for (int i = 0; i < n; ++i)\n      for (int j = 0; j < m; ++j)\n        if (!grid[i][j]) ans += dfs(j, i);      \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a 2D&nbsp;grid&nbsp;consists of&nbsp;0s&nbsp;(land)&nbsp;and&nbsp;1s&nbsp;(water).&nbsp; An&nbsp;island&nbsp;is a maximal 4-directionally connected group of&nbsp;0s&nbsp;and a&nbsp;closed island&nbsp;is an island&nbsp;totally&nbsp;(all left, top, right, bottom) surrounded by&nbsp;1s. Return the number of&nbsp;closed&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[102,33,433,177,516],"class_list":["post-5817","post","type-post","status-publish","format-standard","hentry","category-searching","tag-connected-components","tag-dfs","tag-island","tag-medium","tag-onm","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5817","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=5817"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5817\/revisions"}],"predecessor-version":[{"id":5830,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5817\/revisions\/5830"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}