Snakes and Ladders - Problem

You are given an n x n integer matrix board where the cells are labeled from 1 to in a Boustrophedon style starting from the bottom left of the board (i.e. board[n - 1][0]) and alternating direction each row.

You start on square 1 of the board. In each move, starting from square curr, do the following:

  • Choose a destination square next with a label in the range [curr + 1, min(curr + 6, n²)]. This choice simulates the result of a standard 6-sided die roll.
  • If next has a snake or ladder, you must move to the destination of that snake or ladder. Otherwise, you move to next.
  • The game ends when you reach the square .

A board square on row r and column c has a snake or ladder if board[r][c] != -1. The destination of that snake or ladder is board[r][c].

Squares 1 and are not the starting points of any snake or ladder.

Note: 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 not follow the subsequent snake or ladder.

Return the least number of dice rolls required to reach the square . If it is not possible to reach the square, return -1.

Input & Output

Example 1 — Small Board with Ladder
$ Input: board = [[-1,4],[-1,3]]
Output: 1
💡 Note: Start at 1. Roll die to reach 2, but there's a ladder at position 2 that takes you directly to position 4 (the target). Total moves: 1.
Example 2 — Larger Board
$ 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
💡 Note: 36 cells total. Need minimum 4 dice rolls to reach cell 36, using optimal path that may include ladders and avoiding snakes.
Example 3 — Impossible Case
$ Input: board = [[-1,-1],[-1,1]]
Output: -1
💡 Note: There's a snake at position 4 that brings you back to position 1, creating an infinite loop. Impossible to reach the target.

Constraints

  • n == board.length == board[i].length
  • 2 ≤ n ≤ 20
  • board[i][j] is either -1 or in the range [1, n²]
  • The squares labeled 1 and n² do not have any ladders or snakes

Visualization

Tap to expand
Snakes and Ladders - BFS Approach INPUT 2x2 Board (Boustrophedon) 1 2 3 4 L:3 L:4 --> (row 0) <-- (row 1) board = [[-1,4],[-1,3]] Row 0: [-1, 4] Row 1: [-1, 3] -1 = no snake/ladder 4,3 = ladder destinations Start: 1, Goal: 4 ALGORITHM STEPS 1 Initialize BFS Queue: [(1, 0)] position=1, rolls=0 2 Roll Dice from 1 Can reach: 2, 3, 4 min(1+6, 4) = 4 3 Check Ladders Cell 2 has ladder to 3 Cell 3 has ladder to 4 4 Process Move Roll to 2 --> ladder to 3 --> ladder to 4 (GOAL!) BFS Queue State Init: [(1,0)] Pop 1: try 2,3,4 2-->3-->4 = FOUND! FINAL RESULT Winning Path Found! 1 2 3 4 Start Goal Path Output: 1 Path Summary 1 dice roll needed: Roll to 2 (ladder to 3) 3 has ladder to 4 (GOAL) Key Insight: BFS guarantees shortest path because it explores all positions reachable in k rolls before k+1 rolls. Ladders can chain together! Landing on 2 triggers ladder to 3, which triggers ladder to 4 (goal) - all in ONE roll. Time: O(n²) | Space: O(n²) for visited array TutorialsPoint - Snakes and Ladders | BFS Approach
Asked in
Amazon 15 Microsoft 12 Google 8
32.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen