← Visualizers
Graphs · Grid BFS, shortest path LeetCode 909

Snakes and Ladders / visualized

A classic board game converted to unweighted shortest path: every die roll (1 through 6) costs 1 move. BFS guarantees the minimum die rolls to reach square n². The main gotchas are mapping 1D square numbers to the Boustrophedon 2D grid, taking at most one snake or ladder per turn, and marking seen on push to avoid infinite loop trapdoors!

Board · Wavefront

idle
INVARIANT Snakes & Ladders BFS Invariant: Each die roll (1..6) costs 1 move, so BFS guarantees the minimum moves. Rule: Mark seen[destination] ON PUSH; if a snake/ladder leads to destination d, mark d seen (not the intermediate square), preventing infinite loop trapdoors!
Die Roll: +1 +2 +3 +4 +5 +6
Press Run to begin.
0 / 0
Speed

Java · running line

active cell in queue ladder ↑ snake ↓
INTERVIEW SCRIPT

How to Explain Snakes and Ladders to an Interviewer

1. Problem Formulation as Shortest Path

"Each turn allows 6 possible moves (die roll 1 to 6), each with uniform cost 1. Therefore, finding the minimum number of rolls is an unweighted shortest path problem solved via BFS."

2. Boustrophedon Coordinate Conversion

"Row from top: r = n - 1 - (s - 1) / n. Column flips every other row from bottom: int rem = (s - 1) % n; int c = ((n - 1 - r) % 2 == 1) ? n - 1 - rem : rem;"

3. The Snake / Ladder Invariant

"If a square has a snake or ladder (board[r][c] != -1), we jump immediately to destination d = board[r][c]. Crucially, we mark seen[d] = true on push, NOT the square we landed on before teleporting."

4. Complexity Mastery

"There are $n^2$ squares. Each square is enqueued at most once, and each roll considers 6 edges. Time is strictly $O(n^2)$ and auxiliary space is $O(n^2)$."

O(n²) time, O(n²) space  ·  6 transitions per square  ·  BFS snapshot level represents total die rolls.