Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
sudoku_solver.cpp File Reference

Sudoku Solver algorithm. More...

#include <array>
#include <iostream>
Include dependency graph for sudoku_solver.cpp:

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.
 

Detailed Description

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.

Author
DarthCoder3200
David Leal

Function Documentation

◆ isPossible()

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 Parameters
Vnumber of vertices in the array
Parameters
matmatrix where numbers are saved
icurrent index in rows
jcurrent index in columns
nonumber to be added in matrix
nnumber of times loop will run
Returns
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

45 {
46 /// `no` shouldn't be present in either row i or column j
47 for (int x = 0; x < n; x++) {
48 if (mat[x][j] == no || mat[i][x] == no) {
49 return false;
50 }
51 }
52
53 /// `no` shouldn't be present in the 3*3 subgrid
54 int sx = (i / 3) * 3;
55 int sy = (j / 3) * 3;
56
57 for (int x = sx; x < sx + 3; x++) {
58 for (int y = sy; y < sy + 3; y++) {
59 if (mat[x][y] == no) {
60 return false;
61 }
62 }
63 }
64
65 return true;
66}
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
154 {
155 const int V = 9;
157 std::array<int, V>{5, 3, 0, 0, 7, 0, 0, 0, 0},
158 std::array<int, V>{6, 0, 0, 1, 9, 5, 0, 0, 0},
159 std::array<int, V>{0, 9, 8, 0, 0, 0, 0, 6, 0},
160 std::array<int, V>{8, 0, 0, 0, 6, 0, 0, 0, 3},
161 std::array<int, V>{4, 0, 0, 8, 0, 3, 0, 0, 1},
162 std::array<int, V>{7, 0, 0, 0, 2, 0, 0, 0, 6},
163 std::array<int, V>{0, 6, 0, 0, 0, 0, 2, 8, 0},
164 std::array<int, V>{0, 0, 0, 4, 1, 9, 0, 0, 5},
165 std::array<int, V>{0, 0, 0, 0, 8, 0, 0, 7, 9}};
166
167 backtracking::sudoku_solver::printMat<V>(mat, mat, 9);
168 std::cout << "Solution " << std::endl;
169 std::array<std::array<int, V>, V> starting_mat = mat;
170 backtracking::sudoku_solver::solveSudoku<V>(mat, starting_mat, 0, 0);
171
172 return 0;
173}
T endl(T... args)
Here is the call graph for this function:

◆ printMat()

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 Parameters
Vnumber of vertices in array
Parameters
matmatrix where numbers are saved
starting_matcopy of mat, required by printMat for highlighting the differences
nnumber of times loop will run
Returns
void
78 {
79 for (int i = 0; i < n; i++) {
80 for (int j = 0; j < n; j++) {
81 if (starting_mat[i][j] != mat[i][j]) {
82 std::cout << "\033[93m" << mat[i][j] << "\033[0m"
83 << " ";
84 } else {
85 std::cout << mat[i][j] << " ";
86 }
87 if ((j + 1) % 3 == 0) {
88 std::cout << '\t';
89 }
90 }
91 if ((i + 1) % 3 == 0) {
93 }
95 }
96}
Here is the call graph for this function:

◆ solveSudoku()

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.

Template Parameters
Vnumber of vertices in array
Parameters
matmatrix where numbers are saved
starting_matcopy of mat, required by printMat for highlighting the differences
icurrent index in rows
jcurrent index in columns
Returns
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

112 {
113 /// Base Case
114 if (i == 9) {
115 /// Solved for 9 rows already
116 printMat<V>(mat, starting_mat, 9);
117 return true;
118 }
119
120 /// Crossed the last Cell in the row
121 if (j == 9) {
122 return solveSudoku<V>(mat, starting_mat, i + 1, 0);
123 }
124
125 /// Blue Cell - Skip
126 if (mat[i][j] != 0) {
127 return solveSudoku<V>(mat, starting_mat, i, j + 1);
128 }
129 /// White Cell
130 /// Try to place every possible no
131 for (int no = 1; no <= 9; no++) {
132 if (isPossible<V>(mat, i, j, no, 9)) {
133 /// Place the 'no' - assuming a solution will exist
134 mat[i][j] = no;
135 bool solution_found = solveSudoku<V>(mat, starting_mat, i, j + 1);
136 if (solution_found) {
137 return true;
138 }
139 /// Couldn't find a solution
140 /// loop will place the next `no`.
141 }
142 }
143 /// Solution couldn't be found for any of the numbers provided
144 mat[i][j] = 0;
145 return false;
146}
Here is the call graph for this function: