mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
feat: added highlighting differences in Sudoku Solver (#1438)
* printing out the solved grid with the differences highlighted. * added documentation to the functions. * removed #define because code formatter check failed.
This commit is contained in:
parent
1f5ca99571
commit
9a78d69083
@ -59,14 +59,19 @@ namespace backtracking {
|
|||||||
* Utility function to print matrix
|
* Utility function to print matrix
|
||||||
* @tparam V number of vertices in array
|
* @tparam V number of vertices in array
|
||||||
* @param mat matrix where numbers are saved
|
* @param mat matrix where numbers are saved
|
||||||
|
* @param starting_mat copy of mat, required by printMat for highlighting the differences
|
||||||
* @param n number of times loop will run
|
* @param n number of times loop will run
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
template <size_t V>
|
template <size_t V>
|
||||||
void printMat(const std::array <std::array <int, V>, V> &mat, int n) {
|
void printMat(const std::array <std::array <int, V>, V> &mat, const std::array <std::array <int, V>, V> &starting_mat, int n) {
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
for (int j = 0; j < n; j++) {
|
for (int j = 0; j < n; j++) {
|
||||||
|
if (starting_mat[i][j] != mat[i][j]) {
|
||||||
|
std::cout << "\033[93m" << mat[i][j] << "\033[0m" << " ";
|
||||||
|
} else {
|
||||||
std::cout << mat[i][j] << " ";
|
std::cout << mat[i][j] << " ";
|
||||||
|
}
|
||||||
if ((j + 1) % 3 == 0) {
|
if ((j + 1) % 3 == 0) {
|
||||||
std::cout << '\t';
|
std::cout << '\t';
|
||||||
}
|
}
|
||||||
@ -82,28 +87,29 @@ namespace backtracking {
|
|||||||
* Sudoku algorithm
|
* Sudoku algorithm
|
||||||
* @tparam V number of vertices in array
|
* @tparam V number of vertices in array
|
||||||
* @param mat matrix where numbers are saved
|
* @param mat matrix where numbers are saved
|
||||||
|
* @param starting_mat copy of mat, required by printMat for highlighting the differences
|
||||||
* @param i current index in rows
|
* @param i current index in rows
|
||||||
* @param j current index in columns
|
* @param j current index in columns
|
||||||
* @returns `true` if 'no' was placed
|
* @returns `true` if 'no' was placed
|
||||||
* @returns `false` if 'no' was not placed
|
* @returns `false` if 'no' was not placed
|
||||||
*/
|
*/
|
||||||
template <size_t V>
|
template <size_t V>
|
||||||
bool solveSudoku(std::array <std::array <int, V>, V> &mat, int i, int j) {
|
bool solveSudoku(std::array <std::array <int, V>, V> &mat, const std::array <std::array <int, V>, V> &starting_mat, int i, int j) {
|
||||||
/// Base Case
|
/// Base Case
|
||||||
if (i == 9) {
|
if (i == 9) {
|
||||||
/// Solved for 9 rows already
|
/// Solved for 9 rows already
|
||||||
backtracking::printMat<V>(mat, 9);
|
backtracking::printMat<V>(mat, starting_mat, 9);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Crossed the last Cell in the row
|
/// Crossed the last Cell in the row
|
||||||
if (j == 9) {
|
if (j == 9) {
|
||||||
return backtracking::solveSudoku<V>(mat, i + 1, 0);
|
return backtracking::solveSudoku<V>(mat, starting_mat, i + 1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Blue Cell - Skip
|
/// Blue Cell - Skip
|
||||||
if (mat[i][j] != 0) {
|
if (mat[i][j] != 0) {
|
||||||
return backtracking::solveSudoku<V>(mat, i, j + 1);
|
return backtracking::solveSudoku<V>(mat, starting_mat, i, j + 1);
|
||||||
}
|
}
|
||||||
/// White Cell
|
/// White Cell
|
||||||
/// Try to place every possible no
|
/// Try to place every possible no
|
||||||
@ -111,7 +117,7 @@ namespace backtracking {
|
|||||||
if (backtracking::isPossible<V>(mat, i, j, no, 9)) {
|
if (backtracking::isPossible<V>(mat, i, j, no, 9)) {
|
||||||
/// Place the 'no' - assuming a solution will exist
|
/// Place the 'no' - assuming a solution will exist
|
||||||
mat[i][j] = no;
|
mat[i][j] = no;
|
||||||
bool solution_found = backtracking::solveSudoku<V>(mat, i, j + 1);
|
bool solution_found = backtracking::solveSudoku<V>(mat, starting_mat, i, j + 1);
|
||||||
if (solution_found) {
|
if (solution_found) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -142,9 +148,10 @@ int main() {
|
|||||||
std::array <int, V> {0, 0, 0, 0, 8, 0, 0, 7, 9}
|
std::array <int, V> {0, 0, 0, 0, 8, 0, 0, 7, 9}
|
||||||
};
|
};
|
||||||
|
|
||||||
backtracking::printMat<V>(mat, 9);
|
backtracking::printMat<V>(mat, mat, 9);
|
||||||
std::cout << "Solution " << std::endl;
|
std::cout << "Solution " << std::endl;
|
||||||
backtracking::solveSudoku<V>(mat, 0, 0);
|
std::array <std::array <int, V>, V> starting_mat = mat;
|
||||||
|
backtracking::solveSudoku<V>(mat, starting_mat, 0, 0);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user