Introduction
A naive backtracking solver tries digits cell by cell and can take millions of steps on hard puzzles. This solver keeps, for every row, column and box, a 9-bit mask of used digits, fills every forced cell (naked singles) before guessing, and always guesses in the cell with the fewest options. Hard 17-clue puzzles then need only a couple of thousand guesses.
Problem statement
Objectives
- Represent constraints with bit masks and bit operations
- Combine constraint propagation with backtracking
- Use the minimum-remaining-values heuristic
- Count solutions to test uniqueness
Features
- Flexible puzzle input with titles
- Validation of givens with clear errors
- Uniqueness detection (stops at two solutions)
- Guess counts that show the effect of propagation
Concepts demonstrated
- Backtracking
- Constraint propagation
- Heuristics (MRV)
- Bit masks
- Undoing state
System architecture
Sudoku stores 81 cells and three arrays of masks. candidates(i) is the complement of the row, column and box masks. search first loops over the grid filling cells with exactly one candidate (recording them so they can be undone), then picks the empty cell with the fewest candidates and tries each digit recursively. When no empty cell remains a solution is counted, and the first solution is copied out. Every placement made during a branch is removed before returning, so the grid is restored exactly.
Folder structure
Complete source code
src/Main.java
What each file does
- src/Sudoku.java
- Parsing and validation, bit-mask bookkeeping, the propagate-then-branch search and grid printing.
- src/Main.java
- Reads titled puzzles, solves each and reports uniqueness and guesses.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
# Easy (solved by propagation alone) 530070000 600195000 098000060 800060003 400803001 700020006 060000280 000419005 000080079 # Hard (17 givens) 000000010400000000020000000000050407008000300001090000300400200050100000000806000 # Not unique 000000000000000000000000000000000000000000000000000000000000000000000000000000000 # Contradiction in the givens 553070000600195000098000060800060003400803001700020006060000280000419005000080079 # Unsolvable but legal 123456780000000009000000000000000000000000000000000000000000000000000000000000000 # Too short 12345
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - The easy puzzle is solved by propagation alone (0 guesses).
- The 17-clue puzzle has a unique solution found with 1,918 guesses.
- An empty grid is reported as having more than one solution; contradictory and unsolvable puzzles are recognised.
Common errors
The solver is extremely slow on hard puzzles
Propagate forced cells and branch on the cell with the fewest candidates instead of the first empty cell.
The grid is corrupted after a failed branch
Undo every cell filled during that branch — including the ones filled by propagation.
Possible improvements
- Add hidden-single propagation (a digit with only one place in a unit)
- Generate new puzzles with a unique solution
- Explain the solution step by step for learners
Additional challenges
- 1Solve 16×16 Sudoku with the same code.
- 2Measure guesses with and without MRV.
- 3Solve the puzzle as an exact-cover problem with Dancing Links.
Built and tested it yourself? Mark it to update your dashboard.