Tap here to switch tabs
Problem
Submissions

Problem: Snakes and Ladders

med
30 min
Try to solve the Snakes and Ladders problem.

Statement

You are given an n×nn \times n integer matrix board representing a Snakes and Ladders game. The cells correspond to squares labeled from 11 to n2n^2 in a boustrophedon order: labeling starts at the bottom left cell, proceeds left to right across the bottom row, then continues on the next row up right to left, alternating direction each row until reaching the top row.

The game is played as follows:

  • You start on square 11.

  • In one move, you roll a die and choose a destination square with a label in the range from your current label + 11 to your current label + 66, without exceeding n2n^2.

  • After moving to that square, if the corresponding cell in board contains a value other than 1-1, you must immediately move to the square with that label. Otherwise, you remain on the square you moved to.

Return the minimum number of moves required to reach square n2n^2. If it is not possible to reach square n2n^2, return 1-1.

Note: If the destination of a snake or ladder is the start of another snake or ladder, only the first jump is applied. For example, if 2 → 5 and 5 → 8, landing on 2 moves you to 5, not 8.

Constraints:

  • n == board.length == board[i].length

  • 22 \leq n 20\leq 20

  • board[i][j] is either 1-1 or in the range [1,n2]\left[1, n^2\right]

  • The squares labeled 11 and n2n^2 are not the starting points of any snake or ladder.

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths
Tap here to switch tabs
Problem
Submissions

Problem: Snakes and Ladders

med
30 min
Try to solve the Snakes and Ladders problem.

Statement

You are given an n×nn \times n integer matrix board representing a Snakes and Ladders game. The cells correspond to squares labeled from 11 to n2n^2 in a boustrophedon order: labeling starts at the bottom left cell, proceeds left to right across the bottom row, then continues on the next row up right to left, alternating direction each row until reaching the top row.

The game is played as follows:

  • You start on square 11.

  • In one move, you roll a die and choose a destination square with a label in the range from your current label + 11 to your current label + 66, without exceeding n2n^2.

  • After moving to that square, if the corresponding cell in board contains a value other than 1-1, you must immediately move to the square with that label. Otherwise, you remain on the square you moved to.

Return the minimum number of moves required to reach square n2n^2. If it is not possible to reach square n2n^2, return 1-1.

Note: If the destination of a snake or ladder is the start of another snake or ladder, only the first jump is applied. For example, if 2 → 5 and 5 → 8, landing on 2 moves you to 5, not 8.

Constraints:

  • n == board.length == board[i].length

  • 22 \leq n 20\leq 20

  • board[i][j] is either 1-1 or in the range [1,n2]\left[1, n^2\right]

  • The squares labeled 11 and n2n^2 are not the starting points of any snake or ladder.

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths