Fewest dice rolls to reach the last square of a zig-zag board full of snakes and ladders. Treat squares as nodes and rolls as edges, and breadth-first search finds the answer level by level in O(n²).
▼The problem
LeetCode 909 (Medium). An n × n board's squares are numbered 1 to n² boustrophedon style, from the bottom-left, alternating direction every row. Each move, roll a die and go 1 to 6 squares ahead (never past n²); if you land on a ladder's foot or a snake's head (board[r][c] != -1) you move to its end, at most one jump per move. Return the fewest moves from square 1 to square n², or -1.
Example (LeetCode's example 1, kept from the original): the 6 × 6 board with ladders 2 → 15 and 14 → 35 and a snake 17 → 13 → 4.
The solution
def snakesAndLadders(board):
n = len(board)
def cell(s):
r, c = divmod(s - 1, n)
return n - 1 - r, (c if r % 2 == 0 else n - 1 - c)
q, seen, moves = deque([1]), {1}, 0
while q:
for _ in range(len(q)):
s = q.popleft()
if s == n * n:
return moves
for nxt in range(s + 1, min(s + 6, n * n) + 1):
r, c = cell(nxt)
if board[r][c] != -1:
nxt = board[r][c]
if nxt not in seen:
seen.add(nxt)
q.append(nxt)
moves += 1
return -1Transcript
Snakes and Ladders. An n by n board is numbered from the bottom-left, snaking back and forth, row by row. Each move, roll a die and step one to six squares. Land on a ladder's foot and you climb it; land on a snake's head and you slide down. Find the fewest moves to the last square.
Take this six by six board. A ladder climbs from two to fifteen, another from fourteen to thirty-five, and a snake drops seventeen to thirteen. To place a square, subtract one and divide by six: the quotient is the row, the remainder the column, flipped on every other row.
You could try every sequence of rolls. But each move has six choices, so ten moves is over sixty million sequences, and a snake can send you round the same loop forever.
Instead, treat the board as a graph. Each square is a node, with an edge to each of the next six squares, after any snake or ladder. Every edge is one move, so breadth-first search finds the shortest path: every square one move away, then two, then three.
Let's walk it. Move one reaches three to seven, and the ladder at two lifts us to fifteen. Move two: eight to twelve, sixteen to twenty-one, and the snake drops seventeen to thirteen. Move three: twenty-two to twenty-seven, and from thirteen, the ladder at fourteen reaches thirty-five. Move four: thirty-six. Four moves, and the best route slides down the snake.
In code, a queue starts at square one, with a seen set so no square is queued twice. Each round, pop the whole level and try six rolls: map each square to its row and column, follow any jump, and queue what's new. Then add a move. Popping the last square returns the moves; if the queue runs dry, return minus one.
Each square is queued once and tries six rolls, so time and space are order n squared.
Squares are nodes, rolls are edges, and the search spreads one move at a time. That's Snakes and Ladders.