| File | Date | Author | Commit |
|---|---|---|---|
| .gitignore | 2026-02-09 |
|
[161cf2] Initial commit |
| LICENSE | 2026-02-09 |
|
[2f1af6] Update LICENSE with additional links |
| README.md | 2026-02-09 |
|
[3fdd8b] Update README.md |
| sudoku.cpp | 2026-02-09 |
|
[f245f3] Add files via upload |
| sudoku.h | 2026-02-09 |
|
[f245f3] Add files via upload |
A high-performance C++ Sudoku solver implementing Knuth's Dancing Links (DLX) algorithm — one of the fastest exact cover algorithms in the world.
std::unique_ptr for automatic memory management, eliminating memory leaksThe solver transforms the Sudoku puzzle into an exact cover problem with four constraints:
The DLX algorithm efficiently explores the solution space by:
#include "sudoku.h"
#include <iostream>
int main()
{
try
{
// Create a 9×9 Sudoku solver
// Note: You can also write just SudokuDLXSolver solver;
// since the default size is 9
SudokuDLXSolver solver(9);
// Define a puzzle (0 represents empty cells)
std::vector<std::vector<int>> puzzle = {
{ 0,0,0, 0,0,0, 0,0,0 },
{ 0,0,0, 0,0,3, 0,8,5 },
{ 0,0,1, 0,2,0, 0,0,0 },
{ 0,0,0, 5,0,7, 0,0,0 },
{ 0,0,4, 0,0,0, 1,0,0 },
{ 0,9,0, 0,0,0, 0,0,0 },
{ 5,0,0, 0,0,0, 0,7,3 },
{ 0,0,2, 0,1,0, 0,0,0 },
{ 0,0,0, 0,4,0, 0,0,9 }
};
// Display the original puzzle
std::cout << "Original Puzzle:" << std::endl;
printGrid(puzzle);
// Solve the puzzle
// Note: searchLimit has a default value of 10, so you can also write:
// auto solutions = solver.solve(puzzle);
auto solutions = solver.solve(puzzle, 10);
// Print all found solutions
printSolutions(solutions);
std::cout << "Solutions found: " << solutions.size() << std::endl;
}
catch (const std::exception& e)
{
std::cerr << "ERROR: " << e.what() << std::endl;
return 1;
}
}
SudokuDLXSolver(int size = 9)
Creates a solver for puzzles of the specified size. Size must be a perfect square (4, 9, 16, 25, etc.).
std::vector<std::vector<std::vector<int>>> solve(
const std::vector<std::vector<int>>& puzzle,
int searchLimit = 10
)
puzzle: 2D vector representing the Sudoku grid (0 for empty cells)searchLimit: Maximum number of solutions to find (default: 10)std::invalid_argument if puzzle dimensions are invalidvoid printGrid(const std::vector<std::vector<int>>& grid);
void printSolutions(const std::vector<std::vector<std::vector<int>>>& solutions,
int printLimit = 10);
Dancing Links is an ingenious technique invented by Donald Knuth for efficiently implementing his Algorithm X. The key insights are:
The name "Dancing Links" comes from the way pointers appear to "dance" as they're updated during the covering and uncovering operations.
MIT License
Copyright (c) 2026 Royal_X
https://sourceforge.net/u/royal-x
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
[!NOTE]
This implementation demonstrates that modern C++ can achieve excellent performance with dynamic memory allocation while maintaining safety and expressiveness. The use of smart pointers and RAII ensures memory safety without sacrificing speed.