Modern C++ CLI engine for high-performance Sudoku solving, real-time visualization, and algorithm engineering.
It is a polished open-source portfolio project that combines constraint satisfaction, bit-level optimization, and heuristic search in a clean C++ architecture. The engine is built around a production-grade CLI experience and is designed to showcase both algorithmic rigor and systems programming craftsmanship.
- Sudoku is a compact constraint satisfaction problem that is easy to state but expensive to solve naively.
- Pure backtracking can explode into tens of millions of recursive branches for hard puzzles.
- Modern optimization like Minimum Remaining Values (MRV) and bitmask candidate generation turns Sudoku solving from brute-force into practical performance engineering.
- This repository demonstrates how algorithmic design and low-level C++ efficiency combine to make a solver fast, deterministic, and recruiter-friendly.
| Feature | Description | Status |
|---|---|---|
| Recursive DFS solving | Classic depth-first search with backtracking | β Complete |
| Bitmask constraint engine | Row, column and 3Γ3 box occupancy stored as masks | β Complete |
| MRV heuristic | Chooses the empty cell with the fewest candidates first | β Complete |
| Puzzle validation | Detects invalid boards, duplicates, and unsolvable states | β Complete |
| Visual solve mode | Terminal dashboard with animated step/visualization options | β Complete |
| Puzzle generator | Difficulty-based puzzle creation with uniqueness analysis | β Complete |
| Benchmark mode | Compare solver behavior across modes and difficulty classes | β Complete |
| File I/O | Load/save puzzles, reports, and compact/pretty board formats | β Complete |
| Difficulty analysis | Score-based difficulty estimation and solver report output | β Complete |
- O(1) constraint checking using precomputed row/column/box bitmasks.
- Search-space pruning with MRV to dramatically reduce candidate branching.
- Efficient rollback by restoring only the affected masks and board cell state.
- Heuristic search design: candidate generation is separated from recursive search for clarity and performance.
- CSP engineering: the solver is modeled as a constraint satisfaction pipeline with validation, propagation, solving, and reporting.
The pairing of MRV and bitmask candidates creates a much tighter search space than naive backtracking. Rather than trying every empty cell in order, the solver chooses the most constrained cell and only explores legal digits.
- Naive brute-force backtracking can approach
O(9^m)wheremis the number of empty cells. - Practical performance is orders of magnitude better for real Sudoku puzzles due to early pruning, candidate filtering, and propagation.
- Hard Sudoku puzzles often solve in milliseconds with this approach, while a naive solver may still explore tens of thousands of dead ends.
- Compare
MRV + bitmaskvs. plain sequential backtracking. - Measure solve time across
Easy,Medium,Hard,Expertpuzzle families. - Track recursion depth, total branch count, and backtrack events.
- Profile propagation-enabled solves against pure search-only solves.
[ Sudoku Solver Pro ] Mode: Visual solve Delay: 80ms Propagation: ON
+-------+-------+-------+ [Step 14 / 81]
| 5 3 4 | 6 7 8 | 9 1 2 | Candidates: [2,8]
| 6 7 2 | 1 9 5 | 3 4 8 | Backtracks: 18
| 1 9 8 | 3 4 2 | 5 6 7 |
+-------+-------+-------+
| 8 5 9 | 7 6 1 | 4 2 3 | Status: searching
| 4 2 6 | 8 5 3 | 7 9 1 |
| 7 1 3 | 9 2 4 | 8 5 6 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 6 3 5 |
| 3 4 5 | 2 8 6 | 1 7 9 |
+-------+-------+-------+
Solver statistics
-----------------
Status : Unique solution
Solve time : 0.84 ms
Recursive calls : 1,284
Backtracks : 27
Empty cells : 43
Difficulty score : Expert
Mode comparison
---------------
- MRV + Propagation : 0.84 ms
- MRV only : 1.26 ms
- Plain backtracking: 8.40 ms
Benchmark mode
--------------
Puzzle set : Expert samples
Solver variants: MRV + propagation, MRV only, plain backtracking
Best result : 0.78 ms
Worst result : 12.34 ms
Average solve : 2.17 ms
The solver uses a depth-first search over empty cells. When a candidate fails, it backtracks and restores the board state.
Minimum Remaining Values means the next cell is chosen by the fewest legal candidates. This minimizes branching and prioritizes the hardest decisions first.
Each cell computes its allowed digits with a single bitwise expression:
const uint16_t used = rowMask[r] | colMask[c] | boxMask[b];
const uint16_t candidates = allowedMask & ~used;This avoids scanning all digits and keeps candidate generation constant-time.
When a candidate value does not lead to a solution, the solver restores:
- the board cell to empty
- the row mask
- the column mask
- the box mask
This localized rollback preserves performance and avoids expensive copies.
Sudoku-Solver/
βββ CMakeLists.txt
βββ README.md
βββ LICENSE
βββ main.cpp # CLI orchestration and menu flow
βββ board.cpp/.hpp # board model, parsing, validation, serialization
βββ solver.cpp/.hpp # backtracking engine, MRV, propagation, reports
βββ generator.cpp/.hpp# puzzle generator and difficulty scoring
βββ ui.cpp/.hpp # terminal rendering, dashboard, ANSI helpers
βββ benchmark.cpp/.hpp # solver benchmarking and mode comparison
βββ sample_puzzle.txt # example starting grid
User input -> Board parser -> Validator -> Solver engine -> UI renderer
β
β-> Benchmark / Reports / Generator
Input board
ββ> validate structure
ββ> prepare row/col/box masks
ββ> select MRV cell
ββ> generate candidate bitmask
ββ> recursive DFS search
ββ> solved board / unsolvable state
- Animated visualization
- NCurses-style terminal UI
- Sudoku puzzle generator enhancements
- Parallel solving and multi-threaded benchmark mode
- Dancing Links / Algorithm X implementation
- 16Γ16 variant support
- Replayable solve traces
- GitHub Actions CI and code coverage
g++orclang++with C++20 supportCMake3.20+- Recommended: VS Code with the C++ extension
cd path\to\Sudoku-Solver
g++ -std=c++20 -O2 -Wall -Wextra -pedantic \
main.cpp board.cpp solver.cpp ui.cpp generator.cpp benchmark.cpp \
-o sudoku_solver.exe
.
\sudoku_solver.execd path\to\Sudoku-Solver
cmake -S . -B build
cmake --build build --config Release
.
\build\sudoku_solver.exe- Open the repository folder in VS Code.
- Install the C/C++ extension and CMake Tools.
- Configure a debug target for
sudoku_solver. - Use the integrated terminal for build and run cycles.
- The solver separates UI and engine logic to keep the algorithm portable and testable.
std::arrayand fixed-size buffers are used for predictable memory layout.- Row/column/box masks eliminate dynamic containers and make constraint checks constant-time.
- Backtracking state restoration is intentionally minimal: only the impacted masks and cell are restored.
- Propagation is implemented as a preprocessing pass to accelerate search for harder puzzles.
This project is a strong portfolio asset because it demonstrates:
- DSA fundamentals: backtracking, recursion, complexity analysis.
- Constraint satisfaction: modeling Sudoku as a CSP and applying heuristics.
- Heuristic engineering: MRV plus propagation to optimize search.
- Systems programming: C++20, CLI architecture, build configuration.
- Performance thinking: bitmasking, pruning, benchmarking.
- Modular software design: clean separation of solver, board, UI, generator, benchmark.
Add these assets when the UI is ready:
assets/demo.gifassets/solver-ui.pngassets/benchmark.pngassets/architecture.pngassets/dashboard.png
Contributions are welcome. Please follow these guidelines:
- Open an issue before starting larger work.
- Create a feature branch from
main. - Keep changes focused and add tests or sample puzzles when relevant.
- Use clear commit messages and follow a consistent style.
- Submit a pull request for review.
This project is released under the MIT License. See LICENSE for details.