Skip to content
Problem 909 · LeetCode 909(opens in a new tab)

Snakes and Ladders

MediumBreadth-first search

A game board is an n × n grid board. Its squares are numbered 1 to n² in a zigzag: square 1 is the bottom-left cell, numbers grow to the right along the bottom row, then the next row up counts back from right to left, and so on, switching direction on every row.

You start on square 1. Each move, you roll a die and go forward 1 to 6 squares, never past n²; you pick the roll. If board[r][c] of the square you land on is not −1, it is a snake or ladder and you move to the square it names. You take at most one snake or ladder per roll, even if it drops you on the start of another.

Return the fewest rolls that bring you to square n², or −1 if you can never get there.

Example 1

Input:  board = [[3,-1,-1],[2,-1,8],[-1,-1,-1]]
Output: 2
Why:    roll 3 to climb 4 → 8, then roll 1 to reach 9
2 more examples and the constraints

Example 2

Input:  board = [[2,1,-1],[1,2,1],[-1,-1,2]]
Output: -1
Why:    you can only ever stand on 1 or 2, too far from 9

Example 3

Input:  board = [[-1,-1],[-1,-1]]
Output: 1
Why:    a roll of 3 goes straight from 1 to 4

Constraints

  • 2 ≤ n ≤ 20
  • board[r][c] is −1 or a square number from 1 to n²
  • Squares 1 and n² never hold a snake or ladder

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Relax every square until stableO(n⁴)
  2. Breadth-first searchO(n²)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for a 20 × 20 board, worst caseVerdict
Relax every square until stableO(n⁴)
Passes at n = 20; wasteful
Breadth-first searchO(n²)
Best; each square once

n = side of the board, so there are n² squares.

Edge cases to test

  • Unreachable end. If every square you could land on sends you back, the queue empties: return −1.
  • Ladder onto a ladder. Follow only the first one. The second ladder needs its own roll.
  • A ladder straight to n². Landing on it ends the game; the check d == last catches it.
  • Odd and even n. The top row runs left to right only when n is odd. Derive the column from the row number, not from the top.

Common mistakes

  • Numbering every row left to right, forgetting the zigzag.
  • Following a chain of snakes and ladders in one roll.
  • Marking squares as seen when popped instead of when pushed, so they are queued many times.

How to talk through it

  1. 1Clarify. Can I choose any roll from 1 to 6? Do I follow a second ladder after the first one?
  2. 2Model it as a graph. Squares are nodes; each one links to the six squares its rolls end on.
  3. 3Pick BFS. All edges cost one roll, so breadth-first order is shortest-path order.
  4. 4Code the numbering first. Build dest[s] once, so the search never touches the zigzag again.
  5. 5Test out loud. Run a plain 2 × 2 board, a blocked board and a ladder chain before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?