|
| 1 | +<h2><a href="https://leetcode.com/problems/snakes-and-ladders">Snakes and Ladders</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>You are given an <code>n x n</code> integer matrix <code>board</code> where the cells are labeled from <code>1</code> to <code>n<sup>2</sup></code> in a <a href="https://en.wikipedia.org/wiki/Boustrophedon" target="_blank"><strong>Boustrophedon style</strong></a> starting from the bottom left of the board (i.e. <code>board[n - 1][0]</code>) and alternating direction each row.</p> |
| 2 | + |
| 3 | +<p>You start on square <code>1</code> of the board. In each move, starting from square <code>curr</code>, do the following:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>Choose a destination square <code>next</code> with a label in the range <code>[curr + 1, min(curr + 6, n<sup>2</sup>)]</code>. |
| 7 | + |
| 8 | + <ul> |
| 9 | + <li>This choice simulates the result of a standard <strong>6-sided die roll</strong>: i.e., there are always at most 6 destinations, regardless of the size of the board.</li> |
| 10 | + </ul> |
| 11 | + </li> |
| 12 | + <li>If <code>next</code> has a snake or ladder, you <strong>must</strong> move to the destination of that snake or ladder. Otherwise, you move to <code>next</code>.</li> |
| 13 | + <li>The game ends when you reach the square <code>n<sup>2</sup></code>.</li> |
| 14 | +</ul> |
| 15 | + |
| 16 | +<p>A board square on row <code>r</code> and column <code>c</code> has a snake or ladder if <code>board[r][c] != -1</code>. The destination of that snake or ladder is <code>board[r][c]</code>. Squares <code>1</code> and <code>n<sup>2</sup></code> are not the starting points of any snake or ladder.</p> |
| 17 | + |
| 18 | +<p>Note that you only take a snake or ladder at most once per dice roll. If the destination to a snake or ladder is the start of another snake or ladder, you do <strong>not</strong> follow the subsequent snake or ladder.</p> |
| 19 | + |
| 20 | +<ul> |
| 21 | + <li>For example, suppose the board is <code>[[-1,4],[-1,3]]</code>, and on the first move, your destination square is <code>2</code>. You follow the ladder to square <code>3</code>, but do <strong>not</strong> follow the subsequent ladder to <code>4</code>.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p>Return <em>the least number of dice rolls required to reach the square </em><code>n<sup>2</sup></code><em>. If it is not possible to reach the square, return </em><code>-1</code>.</p> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong class="example">Example 1:</strong></p> |
| 28 | +<img alt="" src="https://assets.leetcode.com/uploads/2018/09/23/snakes.png" style="width: 500px; height: 394px;" /> |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> board = [[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,35,-1,-1,13,-1],[-1,-1,-1,-1,-1,-1],[-1,15,-1,-1,-1,-1]] |
| 31 | +<strong>Output:</strong> 4 |
| 32 | +<strong>Explanation:</strong> |
| 33 | +In the beginning, you start at square 1 (at row 5, column 0). |
| 34 | +You decide to move to square 2 and must take the ladder to square 15. |
| 35 | +You then decide to move to square 17 and must take the snake to square 13. |
| 36 | +You then decide to move to square 14 and must take the ladder to square 35. |
| 37 | +You then decide to move to square 36, ending the game. |
| 38 | +This is the lowest possible number of moves to reach the last square, so return 4. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p><strong class="example">Example 2:</strong></p> |
| 42 | + |
| 43 | +<pre> |
| 44 | +<strong>Input:</strong> board = [[-1,-1],[-1,3]] |
| 45 | +<strong>Output:</strong> 1 |
| 46 | +</pre> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | +<p><strong>Constraints:</strong></p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li><code>n == board.length == board[i].length</code></li> |
| 53 | + <li><code>2 <= n <= 20</code></li> |
| 54 | + <li><code>board[i][j]</code> is either <code>-1</code> or in the range <code>[1, n<sup>2</sup>]</code>.</li> |
| 55 | + <li>The squares labeled <code>1</code> and <code>n<sup>2</sup></code> are not the starting points of any snake or ladder.</li> |
| 56 | +</ul> |
0 commit comments