{"id":4076,"date":"2018-09-25T23:21:21","date_gmt":"2018-09-26T06:21:21","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4076"},"modified":"2018-09-25T23:24:52","modified_gmt":"2018-09-26T06:24:52","slug":"leetcode-909-snakes-and-ladders","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-909-snakes-and-ladders\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 909. Snakes and Ladders"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>On an N x N\u00a0<code>board<\/code>, the numbers from\u00a0<code>1<\/code>\u00a0to\u00a0<code>N*N<\/code>\u00a0are written\u00a0<em>boustrophedonically<\/em>\u00a0<strong>starting from the bottom\u00a0left of the board<\/strong>, and alternating direction each row.\u00a0 For example, for a 6 x 6 board, the numbers are written as follows:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/09\/23\/snakes.png\" alt=\"\" \/><\/p>\n<p>You start on square\u00a0<code>1<\/code>\u00a0of the board (which is always in the last row and\u00a0first column).\u00a0 Each move, starting from square\u00a0<code>x<\/code>, consists of the following:<\/p>\n<ul>\n<li>You choose a destination square\u00a0<code>S<\/code>\u00a0with number\u00a0<code>x+1<\/code>,\u00a0<code>x+2<\/code>,\u00a0<code>x+3<\/code>,\u00a0<code>x+4<\/code>,\u00a0<code>x+5<\/code>, or\u00a0<code>x+6<\/code>, provided this\u00a0number is\u00a0<code>&lt;=\u00a0N*N<\/code>.\n<ul>\n<li>(This choice simulates the result of a standard 6-sided die roll: ie., there are always at most 6 destinations.)<\/li>\n<\/ul>\n<\/li>\n<li>If\u00a0<code>S<\/code>\u00a0has a snake or ladder, you move to the destination of that snake or ladder.\u00a0 Otherwise, you move to\u00a0<code>S<\/code>.<\/li>\n<\/ul>\n<p>A board square on row\u00a0<code>r<\/code>\u00a0and column\u00a0<code>c<\/code>\u00a0has a &#8220;snake or ladder&#8221; if\u00a0<code>board[r][c] != -1<\/code>.\u00a0 The destination of that snake or ladder is\u00a0<code>board[r][c]<\/code>.<\/p>\n<p>Note that you only take a snake or ladder at most once per move: if the destination to a snake or ladder is the start of another\u00a0snake or ladder, you do\u00a0<strong>not<\/strong>\u00a0continue moving.\u00a0 (For example, if the board is `[[4,-1],[-1,3]]`, and on the first move your destination square is `2`, then you finish your first move at\u00a0`3`, because you do\u00a0<strong>not<\/strong>continue moving to `4`.)<\/p>\n<p>Return the least number of moves required to reach square\u00a0<span style=\"font-family: monospace;\">N*N<\/span>.\u00a0 If it is not possible, return\u00a0<code>-1<\/code>.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>[\r\n[-1,-1,-1,-1,-1,-1],\r\n[-1,-1,-1,-1,-1,-1],\r\n[-1,-1,-1,-1,-1,-1],\r\n[-1,35,-1,-1,13,-1],\r\n[-1,-1,-1,-1,-1,-1],\r\n[-1,15,-1,-1,-1,-1]]\r\n<strong>Output: <\/strong>4\r\n<strong>Explanation: <\/strong>\r\nAt the beginning, you start at square 1 [at row 5, column 0].\r\nYou decide to move to square 2, and must take the ladder to square 15.\r\nYou then decide to move to square 17 (row 3, column 5), and must take the snake to square 13.\r\nYou then decide to move to square 14, and must take the ladder to square 35.\r\nYou then decide to move to square 36, ending the game.\r\nIt can be shown that you need at least 4 moves to reach the N*N-th square, so the answer is 4.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>2 &lt;= board.length = board[0].length\u00a0&lt;= 20<\/code><\/li>\n<li><code>board[i][j]<\/code>\u00a0is between\u00a0<code>1<\/code>\u00a0and\u00a0<code>N*N<\/code>\u00a0or is equal to\u00a0<code>-1<\/code>.<\/li>\n<li>The board\u00a0square with number\u00a0<code>1<\/code>\u00a0has no snake or ladder.<\/li>\n<li>The board square with number\u00a0<code>N*N<\/code>\u00a0has no snake or ladder.<\/li>\n<\/ol>\n<h1><strong>Solution: BFS<\/strong><\/h1>\n<p>Time complexity: O(n*n)<\/p>\n<p>Space complexity: O(n*n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua, 8 ms (beats 100%)\r\nclass Solution {\r\npublic:\r\n  int snakesAndLadders(vector&lt;vector&lt;int&gt;&gt;&amp; board) {\r\n    const int N = board.size();\r\n    int steps = 0;\r\n    vector&lt;int&gt; seen(N * N + 1, 0);\r\n    queue&lt;int&gt; q;\r\n    q.push(1);\r\n    seen[1] = 1;\r\n    while (!q.empty()) {\r\n      int size = q.size();\r\n      ++steps;\r\n      while (size--) {\r\n        int s = q.front(); q.pop();        \r\n        for (int x = s + 1; x &lt;= min(s + 6, N * N); ++x) {          \r\n          int r, c;\r\n          getRC(x, N, &amp;r, &amp;c);\r\n          int nx = board[r][c] == -1 ? x : board[r][c];          \r\n          if (seen[nx]) continue;\r\n          if (nx == N * N) return steps;\r\n          q.push(nx);\r\n          seen[nx] = 1;\r\n        } \r\n      }      \r\n    }\r\n    return -1;\r\n  }\r\nprivate:\r\n  void getRC(int s, int N, int* r, int* c) {\r\n    int y = (s - 1) \/ N;\r\n    int x = (s - 1) % N;\r\n    *r = N - 1 - y;\r\n    *c = (y % 2) ?  N - 1 - x : x;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem On an N x N\u00a0board, the numbers from\u00a01\u00a0to\u00a0N*N\u00a0are written\u00a0boustrophedonically\u00a0starting from the bottom\u00a0left of the board, and alternating direction each row.\u00a0 For example, for a&#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":[34,7,177],"class_list":["post-4076","post","type-post","status-publish","format-standard","hentry","category-searching","tag-bfs","tag-board","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4076","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=4076"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4076\/revisions"}],"predecessor-version":[{"id":4080,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4076\/revisions\/4080"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}