A Sudoku puzzle is a logic grid with three iron rules: every row,
every column, and every 3×3 box must contain the
digits 1–9 exactly once. A computer doesn't "see" the pattern — it searches. Below you can watch a solver
try digits, hit dead ends, back up, and eventually fill in the answer — then switch on smarter
reasoning and see the search collapse.
1. Backtracking — guess, check, recurse, back up
The solver walks to the first empty cell and tries 1, 2, 3… For each
candidate it checks the row, column, and box. A digit that fits is placed (it might still be wrong) and the
solver moves on; when a cell runs out of legal digits, it backs up — erasing its
last guess and trying the next number there. This depth-first trial and error is guaranteed to find
the solution if one exists. Type your own puzzle by clicking a cell and pressing 1–9 (or 0 / Backspace to clear).
2. Constraint propagation — think before you guess
Pure backtracking is dumb: it will happily try a digit a human can see is impossible. Constraint
propagation reasons first. Each empty cell carries pencil marks — the digits still
possible there. Place any clue and you erase that digit from its row, column, and box, shrinking neighbours' marks. When a
cell's marks shrink to one digit (a "naked single"), it's forced — fill it in, which shrinks its neighbours,
and the deductions cascade. Watch the candidate counts fall, then compare how many backtracking steps each strategy needs.
Pencil marks shown · click a clue to watch it propagate
—
brute backtracking steps
—
with constraint propagation
0
singles filled by logic alone
3. Why it works — and why it's hard
Sudoku is a constraint-satisfaction problem (CSP): 81 variables (the cells), each with domain
{1…9}, plus the constraint that the 27 groups — 9 rows, 9 columns, 9 boxes — each hold all nine digits
with no repeats. "Solving" means assigning every variable so that no constraint is violated.
Backtracking is depth-first search over those assignments. Try a value, recurse to the next
variable, and the moment a partial assignment can't be completed, abandon it and back up. Because it
prunes whole branches the instant they go illegal, it explores far less than the astronomical 981
of blind enumeration.
Heuristics make it fast. Constraint propagation deduces forced cells so they never need guessing.
Choosing the most-constrained cell first (the empty cell with the fewest remaining candidates — the
"minimum remaining values" heuristic) means each guess has the best odds of being right and dead ends are
hit sooner, slashing the branching factor.
But these are heuristics, not magic. In general, deciding whether an n²×n² Sudoku has a solution is
NP-complete — no known algorithm solves every instance in polynomial time. The 9×9 grids we play are
small enough that a good solver finishes in milliseconds, yet the underlying problem is genuinely hard, which
is exactly why backtracking-plus-propagation is the workhorse approach.