Snakes and Ladders

You are given an n x n integer matrix representing a board for a 'Snakes and Ladders' game. The cells are labeled in a Boustrophedon style, starting from the bottom left, alternating the direction each row. Begin on square 1, rolling a 6-sided die to choose a destination square in a specific range. If the destination square has a snake or ladder, move according to that snake or ladder. The game ends when you reach the final square. Return the minimum number of dice rolls needed to reach that square; if not possible, return -1.

Constraints

  • n == board.length == board[i].length
  • 2 <= n <= 20
  • board[i][j] is either -1 or in the range [1, n^2]
  • The squares labeled 1 and n^2 are not the starting points of any snake or ladder.
Saving… · 3 lines
class Solution:
def snakesAndLadders(self, board: List[List[int]]) -> int:

Console

Click Run to execute sample tests.