{"id":190,"date":"2017-09-09T22:58:43","date_gmt":"2017-09-10T05:58:43","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=190"},"modified":"2018-04-19T08:41:03","modified_gmt":"2018-04-19T15:41:03","slug":"leetcode-63-unique-paths-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-63-unique-paths-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 63. Unique Paths II"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 63. Unique Paths II - \u5237\u9898\u627e\u5de5\u4f5c EP46\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/8v-dX6ato_Y?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<p><strong>Problem:<\/strong><\/p>\n<p>Follow up for &#8220;Unique Paths&#8221;:<\/p>\n<p>Now consider if some obstacles are added to the grids. How many unique paths would there be?<\/p>\n<p>An obstacle and empty space is marked as\u00a0<code>1<\/code>\u00a0and\u00a0<code>0<\/code>\u00a0respectively in the grid.<\/p>\n<p>For example,<\/p>\n<p>There is one obstacle in the middle of a 3&#215;3 grid as illustrated below.<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Dynamic Programming<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/63-ep46.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-192\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/63-ep46.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/63-ep46.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/63-ep46-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/63-ep46-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/63-ep46-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n    int uniquePathsWithObstacles(vector&lt;vector&lt;int&gt;&gt;&amp; obstacleGrid) {\r\n        int n = obstacleGrid.size();\r\n        if (n == 0) return 0;\r\n        int m = obstacleGrid[0].size();\r\n        \r\n        \/\/ f[i][j] = paths(i, j)\r\n        \/\/ INT_MIN -&gt; not solved yet, solution unknown\r\n        f_ = vector&lt;vector&lt;int&gt;&gt;(n + 1, vector&lt;int&gt;(m + 1, INT_MIN));        \r\n        return paths(m, n, obstacleGrid);\r\n    }\r\nprivate:\r\n    vector&lt;vector&lt;int&gt;&gt; f_;\r\n    \r\n    int paths(int x, int y, const vector&lt;vector&lt;int&gt;&gt;&amp; o) {\r\n        \/\/ Out of bound, return 0.\r\n        if (x &lt;= 0 || y &lt;= 0) return 0;\r\n        \r\n        \/\/ Reaching the starting point.\r\n        \/\/ Note, there might be an obstacle here as well.\r\n        if (x == 1 &amp;&amp; y == 1) return 1 - o[0][0];\r\n        \r\n        \/\/ Already solved, return the answer.\r\n        if (f_[y][x] != INT_MIN) return f_[y][x];\r\n        \r\n        \/\/ There is an obstacle on current block, no path\r\n        if (o[y - 1][x - 1] == 1) {\r\n            f_[y][x] = 0;\r\n        } else {\r\n            \/\/ Recursively find paths.\r\n            f_[y][x] = paths(x - 1, y, o) + paths(x, y - 1, o);\r\n        }\r\n        \r\n        \/\/ Return the memorized answer.\r\n        return f_[y][x];\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-62-unique-paths\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 62. Unique Paths<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Follow up for &#8220;Unique Paths&#8221;: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle&#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":[],"class_list":["post-190","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/190","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=190"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/190\/revisions"}],"predecessor-version":[{"id":2707,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/190\/revisions\/2707"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}