77void printMat(
const std::array<std::array<int, V>, V> &mat,
78 const std::array<std::array<int, V>, V> &starting_mat,
int n) {
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"
85 std::cout << mat[i][j] <<
" ";
87 if ((j + 1) % 3 == 0) {
91 if ((i + 1) % 3 == 0) {
92 std::cout << std::endl;
94 std::cout << std::endl;
111 const std::array<std::array<int, V>, V> &starting_mat,
int i,
126 if (mat[i][j] != 0) {
131 for (
int no = 1; no <= 9; no++) {
135 bool solution_found =
solveSudoku<V>(mat, starting_mat, i, j + 1);
136 if (solution_found) {
156 std::array<std::array<int, V>, V> mat = {
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}};
168 std::cout <<
"Solution " << std::endl;
169 std::array<std::array<int, V>, V> starting_mat = mat;
bool 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)
void 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.
bool 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.