Snakes and Ladders
On an n x n board numbered 1..n^2 in a boustrophedon (snake) pattern starting bottom-left, you begin on square 1. Each move rolls a die and advances 1..6 squares. If the destination square holds a snake or ladder (board value != -1), you must move to that value's square. Return the least number of moves to reach square n^2, or -1 if unreachable.
Open official problem prompt ↗Find the fewest dice rolls to travel from square 1 to the final square, obeying the redirections imposed by snakes and ladders.
It is the classic children's board game: count the minimum number of turns to finish, where a well-placed ladder skips you far ahead and a snake drags you back — but you always advance exactly one to six squares per roll before any redirection.
- 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
- Why
- One optimal route in 4 rolls: from 1 roll to 2 (ladder to 15); from 15 roll to 17 (ladder to 13); from 13 roll to 14 (ladder to 35); from 35 roll to 36. That reaches square 36 in 4 total dice rolls.
n == board.length == board[i].length2 <= n <= 20grid values are -1 or in the range [1, n^2]Squares 1 and n^2 are not the start of a snake or ladderA board square has at most one snake or ladder