TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Sudoku Solver algorithm. More...
#include <array>
#include <iostream>
Go to the source code of this file.
Namespaces | |
namespace | backtracking |
for vector container | |
namespace | sudoku_solver |
Functions for the Sudoku Solver implementation. | |
Functions | |
template<size_t V> | |
bool | backtracking::sudoku_solver::isPossible (const std::array< std::array< int, V >, V > &mat, int i, int j, int no, int n) |
Check if it's possible to place a number (no parameter) | |
template<size_t V> | |
void | backtracking::sudoku_solver::printMat (const std::array< std::array< int, V >, V > &mat, const std::array< std::array< int, V >, V > &starting_mat, int n) |
Utility function to print the matrix. | |
template<size_t V> | |
bool | backtracking::sudoku_solver::solveSudoku (std::array< std::array< int, V >, V > &mat, const std::array< std::array< int, V >, V > &starting_mat, int i, int j) |
Main function to implement the Sudoku algorithm. | |
int | main () |
Main function. | |
Sudoku Solver algorithm.
Sudoku (数独, sūdoku, digit-single) (/suːˈdoʊkuː/, /-ˈdɒk-/, /sə-/, originally called Number Place) is a logic-based, combinatorial number-placement puzzle. In classic sudoku, the objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 subgrids that compose the grid (also called "boxes", "blocks", or "regions") contain all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a single solution.
Definition in file sudoku_solver.cpp.
bool backtracking::sudoku_solver::isPossible | ( | const std::array< std::array< int, V >, V > & | mat, |
int | i, | ||
int | j, | ||
int | no, | ||
int | n ) |
Check if it's possible to place a number (no
parameter)
V | number of vertices in the array |
mat | matrix where numbers are saved |
i | current index in rows |
j | current index in columns |
no | number to be added in matrix |
n | number of times loop will run |
true
if 'mat' is different from 'no' false
if 'mat' equals to 'no' no
shouldn't be present in either row i or column j
no
shouldn't be present in the 3*3 subgrid
Definition at line 44 of file sudoku_solver.cpp.
int main | ( | void | ) |
Main function.
Definition at line 154 of file sudoku_solver.cpp.
void backtracking::sudoku_solver::printMat | ( | const std::array< std::array< int, V >, V > & | mat, |
const std::array< std::array< int, V >, V > & | starting_mat, | ||
int | n ) |
Utility function to print the matrix.
V | number of vertices in array |
mat | matrix where numbers are saved |
starting_mat | copy of mat, required by printMat for highlighting the differences |
n | number of times loop will run |
Definition at line 77 of file sudoku_solver.cpp.
bool backtracking::sudoku_solver::solveSudoku | ( | std::array< std::array< int, V >, V > & | mat, |
const std::array< std::array< int, V >, V > & | starting_mat, | ||
int | i, | ||
int | j ) |
Main function to implement the Sudoku algorithm.
V | number of vertices in array |
mat | matrix where numbers are saved |
starting_mat | copy of mat, required by printMat for highlighting the differences |
i | current index in rows |
j | current index in columns |
true
if 'no' was placed false
if 'no' was not placed Base Case
Solved for 9 rows already
Crossed the last Cell in the row
Blue Cell - Skip
White Cell Try to place every possible no
Place the 'no' - assuming a solution will exist
Couldn't find a solution loop will place the next no
.
Solution couldn't be found for any of the numbers provided
Definition at line 110 of file sudoku_solver.cpp.