TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
sudoku_solver.cpp
Go to the documentation of this file.
1
18#include <array>
19#include <iostream>
20
25namespace backtracking {
31namespace sudoku_solver {
43template <size_t V>
44bool isPossible(const std::array<std::array<int, V>, V> &mat, int i, int j,
45 int no, int n) {
47 for (int x = 0; x < n; x++) {
48 if (mat[x][j] == no || mat[i][x] == no) {
49 return false;
50 }
51 }
52
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}
76template <size_t V>
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"
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) {
92 std::cout << std::endl;
93 }
94 std::cout << std::endl;
95 }
96}
97
109template <size_t V>
110bool solveSudoku(std::array<std::array<int, V>, V> &mat,
111 const std::array<std::array<int, V>, V> &starting_mat, int i,
112 int j) {
114 if (i == 9) {
116 printMat<V>(mat, starting_mat, 9);
117 return true;
118 }
119
121 if (j == 9) {
122 return solveSudoku<V>(mat, starting_mat, i + 1, 0);
123 }
124
126 if (mat[i][j] != 0) {
127 return solveSudoku<V>(mat, starting_mat, i, j + 1);
128 }
131 for (int no = 1; no <= 9; no++) {
132 if (isPossible<V>(mat, i, j, no, 9)) {
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 }
141 }
142 }
144 mat[i][j] = 0;
145 return false;
146}
147} // namespace sudoku_solver
148} // namespace backtracking
149
154int main() {
155 const int V = 9;
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}};
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}
for vector container
Functions for the Sudoku Solver implementation.
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.
int main()
Main function.