InterviewPrepKit

Home / Coding / Graphs

Snakes and Ladders

medium Original β†—
Solving tips
  • Model each cell as a node with unit-cost edges to cells x+1..x+6; fewest moves on an unweighted graph means BFS, so the first arrival at n^2 is optimal; O(n^2).
  • The fiddly part is the boustrophedon mapping: use divmod(cell-1, n), then row = n-1-quot and the quotient's parity decides left-to-right vs right-to-left column order.
  • Apply any snake/ladder at the landing cell exactly once (no chaining into a second), and mark the final destination visited, not the intermediate rolled-onto cell.
  • Pitfall: break when nxt > n^2 to skip illegal overshoot rolls, and test the coordinate mapping on a tiny board before trusting it.

Problem

You’re given an n x n board. The cells are numbered 1 to nΒ² in a boustrophedon (β€œox-plowing”) layout: numbering starts at the bottom-left corner, runs left-to-right along the bottom row, then the direction flips on each row moving upward.

You start on cell 1. On each move you roll a die and advance to any cell in [current + 1, current + 6] that exists on the board. If that landing cell holds a snake or ladder, you immediately move to its destination β€” but you take at most one snake/ladder per move (you do not chain into a second one from the destination).

board[r][c] is -1 for an ordinary cell, or the destination cell number of a snake/ladder. Return the least number of moves to reach cell nΒ², or -1 if it’s unreachable.

Examples

  • board = [[-1,-1],[-1,3]] β†’ 1 β€” cell 2 is a ladder to 3, but from cell 1 you can also roll a 3 straight to cell 4 (nΒ²) in one move.
  • 6Γ—6 board with board[5][1]=15 (cell 2β†’15), board[3][1]=35 (cell 14β†’35), board[3][4]=13 (cell 17β†’13) β†’ 4 β€” 1 β†’2(↑15) β†’17(↓13) β†’14(↑35) β†’36.
  • board = [[-1,-1,-1],[-1,9,8],[-1,8,9]] β†’ 1 β€” from cell 1, reaching cell 2 or beyond and taking a ladder lands you on 9 (nΒ²) in one move.

Constraints

  • 2 <= n <= 20, so at most 400 cells.
  • Every move has the same cost (one roll), which is the key to the expected complexity.
  • A cell that is itself a snake/ladder head is never a valid resting square β€” you only stop on its destination.

Think about it first

Hint 1 Model each cell as a graph node. From cell x there is an edge to each of x+1 … x+6 (following any snake/ladder at the landing cell). Every edge costs one move β€” so this is a shortest-path problem on an unweighted graph.
Hint 2 Shortest path with uniform edge cost is exactly what breadth-first search computes: the first time BFS reaches a node, it does so in the fewest moves. DFS would explore paths but can't guarantee the minimum without exhaustively trying everything.
Hint 3 The only fiddly part is converting a 1-based cell number to a (row, col) on the boustrophedon board. Use divmod(cell - 1, n): the quotient tells you how many rows up from the bottom, and its parity tells you whether that row runs left-to-right or right-to-left.
Your workspace Not runnable by design β€” this is your interview scratchpad. Saved on this device.