Sudoku Solver
Enter a puzzle, click Solve, or generate a new one. Backtracking algorithm with visual animation.
How to Use the Sudoku Solver
- Enter your puzzle — click any cell and type digits 1–9. Empty cells represent blanks.
- Choose an action — Solve fills in the solution, Validate highlights conflicts, Generate creates a fresh puzzle.
- Watch the animation — the solver visually fills cells as it works through the backtracking tree.
- Read the status — see how long solving took and how many backtracks were needed.
About the Backtracking Algorithm
Backtracking is a classic constraint-satisfaction algorithm used to solve combinatorial puzzles like Sudoku. The algorithm works by iterating through empty cells, trying each digit from 1 to 9, and checking whether the placement violates any rule — no duplicate in the same row, column, or 3x3 box. If a valid digit is found, the algorithm recurses to the next empty cell. If no digit works, the algorithm backtracks to the previous cell and tries the next candidate. This process continues until either a complete valid solution is found or all possibilities are exhausted.
Constraint Propagation
Before trying digits, the solver narrows down the candidate set for each cell using constraint propagation. If a row already contains digits 1–8, the only possible value for the empty cell is 9. This dramatically reduces the search space and makes most puzzles solvable in under 1 millisecond. Hard puzzles designed to defeat naive backtracking may take slightly longer, but still complete in under 100 milliseconds.
Puzzle Difficulty
The difficulty of a Sudoku puzzle is primarily determined by how many cells are pre-filled and which solving techniques are required. Easy puzzles can be solved by a human using only naked singles — cells where only one digit is possible. Medium puzzles require hidden singles, where a digit can only go in one place within a row, column, or box. Hard puzzles require more advanced techniques like naked pairs, pointing pairs, X-Wings, or Swordfish. All difficulty levels are trivial for a computer solver.
Sudoku Rules
- Every row must contain each digit 1–9 exactly once
- Every column must contain each digit 1–9 exactly once
- Every 3x3 box must contain each digit 1–9 exactly once
- A valid puzzle must have exactly one solution
History of Sudoku
Sudoku was popularized in Japan by Nikoli magazine in 1986 under the name "Sudoku" (meaning "single numbers"). It was introduced to the Western world by Wayne Gould, a retired judge from New Zealand, who wrote a computer program to generate puzzles and brought them to The Times newspaper in London in 2004. The puzzle became an international phenomenon almost overnight. While the modern puzzle is Japanese in origin, the mathematical concept of Latin squares — grids where each symbol appears once per row and column — dates back to Leonhard Euler in the 18th century. The addition of the 3x3 box constraint is what makes Sudoku uniquely challenging and different from a simple Latin square.
Applications of Constraint Satisfaction
The backtracking technique used here has applications far beyond Sudoku. It is used in compiler register allocation, scheduling problems, map coloring, and cryptarithmetic. Modern SAT solvers — which power formal verification in chip design — are sophisticated extensions of backtracking with learned clauses and non-chronological backjumping. Understanding backtracking through Sudoku is an excellent entry point into the broader world of constraint programming and artificial intelligence.