{"id":5437,"date":"2019-08-17T21:45:18","date_gmt":"2019-08-18T04:45:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5437"},"modified":"2019-08-17T21:51:06","modified_gmt":"2019-08-18T04:51:06","slug":"leetcode-1162-as-far-from-land-as-possible","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1162-as-far-from-land-as-possible\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1162. As Far from Land as Possible"},"content":{"rendered":"\n<p>Given an N x N&nbsp;<code>grid<\/code>&nbsp;containing only values&nbsp;<code>0<\/code>&nbsp;and&nbsp;<code>1<\/code>, where&nbsp;<code>0<\/code>&nbsp;represents water&nbsp;and&nbsp;<code>1<\/code>&nbsp;represents land, find a water cell such that its distance to the nearest land cell is maximized and return the distance.<\/p>\n\n\n\n<p>The distance used in this problem is the&nbsp;<em>Manhattan distance<\/em>:&nbsp;the distance between two cells&nbsp;<code>(x0, y0)<\/code>&nbsp;and&nbsp;<code>(x1, y1)<\/code>is&nbsp;<code>|x0 - x1| + |y0 - y1|<\/code>.<\/p>\n\n\n\n<p>If no land or water exists in the grid, return&nbsp;<code>-1<\/code>.<\/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\/05\/03\/1336_ex1.JPG\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong>[[1,0,1],[0,0,0],[1,0,1]]\n<strong>Output: <\/strong>2\n<strong>Explanation: <\/strong>\nThe cell (1, 1) is as far as possible from all the land with distance 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\/2019\/05\/03\/1336_ex2.JPG\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong>[[1,0,0],[0,0,0],[0,0,0]]\n<strong>Output: <\/strong>4\n<strong>Explanation: <\/strong>\nThe cell (2, 2) is as far as possible from all the land with distance 4.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= grid.length == grid[0].length&nbsp;&lt;= 100<\/code><\/li><li><code>grid[i][j]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: BFS<\/strong><\/h2>\n\n\n\n<p>Put all land cells into a queue as source nodes and BFS for water cells, the last expanded one will be the farthest.<\/p>\n\n\n\n<p>Time compleixty: O(n^2)<br>Space complexity: O(n^2)<\/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, 48 ms, 13.4 MB\nclass Solution {\npublic:\n  int maxDistance(vector<vector<int>>& grid) {\n    const int n = grid.size();\n    const int m = grid[0].size();\n    int ans = -1;\n    queue<int> q; \/\/ y*100 + x\n    for (int i = 0; i < n; ++i)\n      for (int j = 0; j < m; ++j)\n        if (grid[i][j] == 1) q.push(i * 100 + j);\n    vector<int> dirs{0, -1, 0, 1, 0};\n    int steps = 0;\n    while (q.size()) {\n      int size = q.size();\n      while (size--) {\n        auto p = q.front(); q.pop();\n        int x = p % 100;\n        int y = p \/ 100;\n        if (grid[y][x] == 2)\n          ans = max(ans, steps);\n        for (int i = 0; i < 4; ++i) {\n          int tx = x + dirs[i];\n          int ty = y + dirs[i + 1];\n          if (tx < 0 || tx >= m || ty < 0 || ty >= n || grid[ty][tx] != 0) continue;\n          grid[ty][tx] = 2;\n          q.push(ty * 100 + tx);\n        }\n      }\n      ++steps;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an N x N&nbsp;grid&nbsp;containing only values&nbsp;0&nbsp;and&nbsp;1, where&nbsp;0&nbsp;represents water&nbsp;and&nbsp;1&nbsp;represents land, find a water cell such that its distance to the nearest land cell is maximized&#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,77,177,42],"class_list":["post-5437","post","type-post","status-publish","format-standard","hentry","category-graph","tag-bfs","tag-graph","tag-medium","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5437","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=5437"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5437\/revisions"}],"predecessor-version":[{"id":5440,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5437\/revisions\/5440"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}