{"id":3130,"date":"2018-07-14T08:43:26","date_gmt":"2018-07-14T15:43:26","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3130"},"modified":"2018-07-14T09:21:28","modified_gmt":"2018-07-14T16:21:28","slug":"leetcode-542-01-matrix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-542-01-matrix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 542. 01 Matrix"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.<\/p>\n<p>The distance between two adjacent cells is 1.<\/p>\n<p><b>Example 1:\u00a0<\/b><br \/>\nInput:<\/p>\n<pre class=\"crayon:false\">0 0 0\r\n0 1 0\r\n0 0 0\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"crayon:false\">0 0 0\r\n0 1 0\r\n0 0 0\r\n<\/pre>\n<p><b>Example 2:\u00a0<\/b><br \/>\nInput:<\/p>\n<pre class=\"crayon:false\">0 0 0\r\n0 1 0\r\n1 1 1\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"crayon:false\">0 0 0\r\n0 1 0\r\n1 2 1\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The number of elements of the given matrix will not exceed 10,000.<\/li>\n<li>There are at least one 0 in the given matrix.<\/li>\n<li>The cells are adjacent in only four directions: up, down, left and right.<\/li>\n<\/ol>\n<h1><strong>Solution 1: DP<\/strong><\/h1>\n<p>Two passes:<\/p>\n<ol>\n<li>down, right<\/li>\n<li>up, left<\/li>\n<\/ol>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(mn)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 132 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; updateMatrix(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {    \r\n    int m = matrix.size();\r\n    int n = matrix[0].size();\r\n    vector&lt;vector&lt;int&gt;&gt; ans(m, vector&lt;int&gt;(n, INT_MAX - m * n));\r\n    for (int i = 0; i &lt; m; ++i)\r\n      for (int j = 0; j &lt; n; ++j)\r\n        if (matrix[i][j]) {\r\n          if (i &gt; 0) ans[i][j] = min(ans[i][j], ans[i - 1][j] + 1);\r\n          if (j &gt; 0) ans[i][j] = min(ans[i][j], ans[i][j - 1] + 1);\r\n        } else {\r\n          ans[i][j] = 0;\r\n        }\r\n    for (int i = m - 1; i &gt;= 0; --i)\r\n      for (int j = n - 1; j &gt;= 0; --j) {\r\n        if (i &lt; m - 1) ans[i][j] = min(ans[i][j], ans[i + 1][j] + 1);\r\n        if (j &lt; n - 1) ans[i][j] = min(ans[i][j], ans[i][j + 1] + 1);\r\n      }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<h1><strong>Solution 2: BFS<\/strong><\/h1>\n<p>Start from all 0 cells and find shortest paths to rest of the cells.<\/p>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(mn)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 136 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; updateMatrix(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {\r\n    const int m = matrix.size();\r\n    const int n = matrix[0].size();\r\n    queue&lt;pair&lt;int, int&gt;&gt; q;\r\n    vector&lt;vector&lt;int&gt;&gt; seen(m, vector&lt;int&gt;(n, 0));\r\n    vector&lt;vector&lt;int&gt;&gt; ans(m, vector&lt;int&gt;(n, INT_MAX));\r\n    \r\n    for (int i = 0; i &lt; m; ++i)\r\n      for (int j = 0; j &lt; n; ++j)\r\n        if (matrix[i][j] == 0) {          \r\n          q.push({i, j});\r\n          seen[i][j] = 1;        \r\n        }\r\n    \r\n    vector&lt;int&gt; dirs{0, -1, 0, 1, 0};\r\n    int steps = 0;\r\n    while (!q.empty()) {\r\n      int size = q.size();\r\n      while (size--) {\r\n        auto pair = q.front(); q.pop();\r\n        int i = pair.first;\r\n        int j = pair.second;\r\n        ans[i][j] = steps;\r\n        for (int k = 0; k &lt; 4; ++k) {\r\n          int x = j + dirs[k];\r\n          int y = i + dirs[k + 1];\r\n          if (x &lt; 0 || x &gt;= n || y &lt; 0 || y &gt;= m || seen[y][x]) continue;\r\n          seen[y][x] = 1;\r\n          q.push({y, x});\r\n        }\r\n      }\r\n      ++steps;\r\n    }\r\n    \r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance between two adjacent cells&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[34,18,216,177,87],"class_list":["post-3130","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-bfs","tag-dp","tag-matrix","tag-medium","tag-shortest-path","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3130","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=3130"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3130\/revisions"}],"predecessor-version":[{"id":3134,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3130\/revisions\/3134"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}