{"id":2876,"date":"2018-06-02T10:02:00","date_gmt":"2018-06-02T17:02:00","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2876"},"modified":"2018-07-24T21:51:37","modified_gmt":"2018-07-25T04:51:37","slug":"leetcode-576-out-of-boundary-paths","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-576-out-of-boundary-paths\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 576. Out of Boundary Paths"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 576. Out of Boundary Paths - \u5237\u9898\u627e\u5de5\u4f5c EP194\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/92zh6XvqEgc?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>There is an\u00a0<b>m<\/b>\u00a0by\u00a0<b>n<\/b>\u00a0grid with a ball. Given the start coordinate\u00a0<b>(i,j)<\/b>\u00a0of the ball, you can move the ball to\u00a0<b>adjacent<\/b>\u00a0cell or cross the grid boundary in four directions (up, down, left, right). However, you can\u00a0<b>at most<\/b>\u00a0move\u00a0<b>N<\/b>\u00a0times. Find out the number of paths to move the ball out of grid boundary. The answer may be very large, return it after mod 10<sup>9<\/sup>\u00a0+ 7.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>m = 2, n = 2, N = 2, i = 0, j = 0\r\n<b>Output:<\/b> 6\r\n<b>Explanation:<\/b>\r\n<img decoding=\"async\" src=\"https:\/\/leetcode.com\/static\/images\/problemset\/out_of_boundary_paths_1.png\" width=\"40%\" \/>\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>m = 1, n = 3, N = 3, i = 0, j = 1\r\n<b>Output:<\/b> 12\r\n<b>Explanation:<\/b>\r\n<img decoding=\"async\" src=\"https:\/\/leetcode.com\/static\/images\/problemset\/out_of_boundary_paths_2.png\" width=\"37%\" \/>\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>Once you move the ball out of boundary, you cannot move it back.<\/li>\n<li>The length and height of the grid is in range [1,50].<\/li>\n<li>N is in range [0,50].<\/li>\n<\/ol>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2881\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/576-ep194.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/576-ep194.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/576-ep194-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/576-ep194-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1>Solution<\/h1>\n<p>Time complexity: O(m*n + N * m * n)<\/p>\n<p>Space complexity: O(N*m*n) -&gt; O(m*n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 19 ms\r\nclass Solution {\r\npublic:\r\n  int findPaths(int m, int n, int N, int i, int j) {\r\n    constexpr int kMod = 1000000007;\r\n    vector&lt;vector&lt;vector&lt;int&gt;&gt;&gt; dp(N + 1, vector&lt;vector&lt;int&gt;&gt;(m, vector&lt;int&gt;(n, 0)));\r\n    vector&lt;int&gt; dirs{-1, 0, 1, 0, -1};\r\n    for (int s = 1; s &lt;= N; ++s)\r\n      for (int y = 0; y &lt; m; ++y)\r\n        for (int x = 0; x &lt; n; ++x)\r\n          for (int d = 0; d &lt; 4; ++d) {\r\n            int tx = x + dirs[d];\r\n            int ty = y + dirs[d + 1];            \r\n            if (tx &lt; 0 || ty &lt; 0 || tx &gt;= n || ty &gt;= m)\r\n              dp[s][y][x] += 1;\r\n            else\r\n              dp[s][y][x] = (dp[s][y][x] + dp[s - 1][ty][tx]) % kMod;\r\n          }        \r\n    return dp[N][i][j];\r\n  }\r\n};<\/pre>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  int findPaths(int m, int n, int N, int i, int j) {\r\n    constexpr int kMod = 1000000007;\r\n    vector&lt;vector&lt;int&gt;&gt; dp(m, vector&lt;int&gt;(n, 0));\r\n    vector&lt;int&gt; dirs{-1, 0, 1, 0, -1};\r\n    for (int s = 1; s &lt;= N; ++s) {\r\n      vector&lt;vector&lt;int&gt;&gt; tmp(m, vector&lt;int&gt;(n, 0));\r\n      for (int y = 0; y &lt; m; ++y)\r\n        for (int x = 0; x &lt; n; ++x)\r\n          for (int d = 0; d &lt; 4; ++d) {\r\n            int tx = x + dirs[d];\r\n            int ty = y + dirs[d + 1];            \r\n            if (tx &lt; 0 || ty &lt; 0 || tx &gt;= n || ty &gt;= m)\r\n              tmp[y][x] += 1;\r\n            else\r\n              tmp[y][x] = (tmp[y][x] + dp[ty][tx]) % kMod;\r\n          }\r\n      dp.swap(tmp);\r\n    }\r\n    return dp[i][j];\r\n  }\r\n};<\/pre>\n<p>Recursion with memorization<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int findPaths(int m, int n, int N, int i, int j) {    \r\n    m_ = m;\r\n    n_ = n;\r\n    dp_ = vector&lt;vector&lt;vector&lt;int&gt;&gt;&gt;(N + 1, vector&lt;vector&lt;int&gt;&gt;(m, vector&lt;int&gt;(n, INT_MIN)));    \r\n    return paths(N, j, i);    \r\n  }\r\nprivate:\r\n  vector&lt;vector&lt;vector&lt;int&gt;&gt;&gt; dp_;\r\n  int m_;\r\n  int n_;\r\n  \r\n  \/\/ number of paths starts from out-of-boundary to (x, y) by moving at most N steps.\r\n  int paths(int N, int x, int y) {\r\n    static vector&lt;int&gt; dirs{-1, 0, 1, 0, -1};\r\n    static const int kMod = 1000000007;\r\n    \/\/ Out of boundary, one path.\r\n    if (x &lt; 0 || x &gt;= n_ || y &lt; 0 || y &gt;= m_) return 1;\r\n    \/\/ Can not move but still in the grid, no path.\r\n    if (N == 0) return 0;\r\n    \/\/ Already computed.\r\n    if (dp_[N][y][x] != INT_MIN) return dp_[N][y][x];\r\n    \r\n    dp_[N][y][x] = 0;\r\n    \r\n    for (int d = 0; d &lt; 4; ++d) {\r\n      int tx = x + dirs[d];\r\n      int ty = y + dirs[d + 1];\r\n      dp_[N][y][x] = (dp_[N][y][x] + paths(N - 1, tx, ty)) % kMod;\r\n    }        \r\n    \r\n    return dp_[N][y][x];\r\n  }\r\n};<\/pre>\n<p>no loops<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int findPaths(int m, int n, int N, int i, int j) {    \r\n    m_ = m;\r\n    n_ = n;\r\n    dp_ = vector&lt;vector&lt;vector&lt;int&gt;&gt;&gt;(N + 1, vector&lt;vector&lt;int&gt;&gt;(m, vector&lt;int&gt;(n, INT_MIN)));    \r\n    return paths(N, j, i);    \r\n  }\r\nprivate:\r\n  vector&lt;vector&lt;vector&lt;int&gt;&gt;&gt; dp_;\r\n  int m_;\r\n  int n_;\r\n  \r\n  \/\/ number of paths start from (x, y) and move at most N steps.\r\n  int paths(int N, int x, int y) {    \r\n    static const int kMod = 1000000007;\r\n    \r\n    if (x &lt; 0 || x &gt;= n_ || y &lt; 0 || y &gt;= m_) return 1;\r\n    if (N == 0) return 0;\r\n    if (dp_[N][y][x] != INT_MIN) return dp_[N][y][x];\r\n    \r\n    long ans = 0;\r\n    ans += paths(N - 1, x + 1, y);\r\n    ans += paths(N - 1, x - 1, y);\r\n    ans += paths(N - 1, x, y + 1);\r\n    ans += paths(N - 1, x, y - 1);\r\n    ans %= kMod;\r\n    \r\n    return dp_[N][y][x] = ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-62-unique-paths\/\">\u82b1\u82b1\u9171 LeetCode 62. Unique Paths<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-63-unique-paths-ii\/\">\u82b1\u82b1\u9171 LeetCode 63. Unique Paths II<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/688-knight-probability-in-chessboard\/\">\u82b1\u82b1\u9171 688. Knight Probability in Chessboard<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem There is an\u00a0m\u00a0by\u00a0n\u00a0grid with a ball. Given the start coordinate\u00a0(i,j)\u00a0of the ball, you can move the ball to\u00a0adjacent\u00a0cell or cross the grid boundary in&#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":[8,18,177,116],"class_list":["post-2876","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-counting","tag-dp","tag-medium","tag-path","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2876","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=2876"}],"version-history":[{"count":10,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2876\/revisions"}],"predecessor-version":[{"id":3289,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2876\/revisions\/3289"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}