#909
Snakes and Ladders
specialist · 935 · lc medium +32 · verified · 48% accepted · 3,635 likes · top 33%
Description
You play on an n x n board whose squares are numbered 1 to n2 in boustrophedon order (alternating left-to-right and right-to-left) from the bottom-left corner. Starting at square 1, each turn you roll a six-sided die and move 1–6 squares forward; if the destination holds a value other than -1, you are transported there (a snake or ladder, applied once per roll). Return the minimum dice rolls needed to reach square n2, or -1 if it is impossible.
Example 1:
Input: 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]]
Output: 4
Explanation:
In the beginning, you start at square 1 (at row 5, column 0).
You decide to move to square 2 and must take the ladder to square 15.
You then decide to move to square 17 and must take the snake to square 13.
You then decide to move to square 14 and must take the ladder to square 35.
You then decide to move to square 36, ending the game.
This is the lowest possible number of moves to reach the last square, so return 4.
Example 2:
Input: board = [[-1,-1],[-1,3]]
Output: 1
Code
1
2
3