From 9cc3951dba866648aa76fa2280abd85ad7962ba0 Mon Sep 17 00:00:00 2001 From: David Leal Date: Thu, 5 Aug 2021 13:47:05 -0500 Subject: [PATCH] [feat/fix/docs]: Improvements in the... ...`backtracking` folder, and minor fixes in the `others/iterative_tree_traversals.cpp` and the `math/check_prime.cpp` files. --- backtracking/graph_coloring.cpp | 142 ++++++------ backtracking/knight_tour.cpp | 121 +++++----- backtracking/minimax.cpp | 17 +- backtracking/n_queens.cpp | 163 ++++++------- .../n_queens_all_solution_optimised.cpp | 4 +- backtracking/nqueen_print_all_solutions.cpp | 17 +- backtracking/rat_maze.cpp | 17 +- backtracking/sudoku_solve.cpp | 217 +++++++++--------- math/check_prime.cpp | 6 +- others/iterative_tree_traversals.cpp | 15 +- 10 files changed, 376 insertions(+), 343 deletions(-) diff --git a/backtracking/graph_coloring.cpp b/backtracking/graph_coloring.cpp index f2ac572c5..8e743963b 100644 --- a/backtracking/graph_coloring.cpp +++ b/backtracking/graph_coloring.cpp @@ -15,83 +15,93 @@ * @author [Anup Kumar Panwar](https://github.com/AnupKumarPanwar) * @author [David Leal](https://github.com/Panquesito7) */ -#include -#include -#include +#include /// for IO operations +#include /// for std::array +#include /// for std::vector /** - * @namespace + * @namespace backtracking * @brief Backtracking algorithms */ namespace backtracking { - /** A utility function to print solution - * @tparam V number of vertices in the graph - * @param color array of colors assigned to the nodes - */ - template - void printSolution(const std::array & color) { - std::cout << "Following are the assigned colors\n"; - for (auto &col : color) { - std::cout << col; - } - std::cout << "\n"; +/** + * @namespace graph_coloring + * @brief Functions for the [Graph Coloring](https://en.wikipedia.org/wiki/Graph_coloring) algorith, + */ +namespace graph_coloring { +/** + * @brief A utility function to print the solution + * @tparam V number of vertices in the graph + * @param color array of colors assigned to the nodes + */ +template +void printSolution(const std::array & color) { + std::cout << "Following are the assigned colors\n"; + for (auto &col : color) { + std::cout << col; } + std::cout << "\n"; +} - /** A utility function to check if the current color assignment is safe for - * vertex v - * @tparam V number of vertices in the graph - * @param v index of graph vertex to check - * @param graph matrix of graph nonnectivity - * @param color vector of colors assigned to the graph nodes/vertices - * @param c color value to check for the node `v` - * @returns `true` if the color is safe to be assigned to the node - * @returns `false` if the color is not safe to be assigned to the node - */ - template - bool isSafe(int v, const std::array, V>& graph, const std::array & color, int c) { - for (int i = 0; i < V; i++) { - if (graph[v][i] && c == color[i]) { - return false; - } - } - return true; - } - - /** A recursive utility function to solve m coloring problem - * @tparam V number of vertices in the graph - * @param graph matrix of graph nonnectivity - * @param m number of colors - * @param [in,out] color description // used in,out to notify in documentation - * that this parameter gets modified by the function - * @param v index of graph vertex to check - */ - template - void graphColoring(const std::array, V>& graph, int m, std::array color, int v) { - // base case: - // If all vertices are assigned a color then return true - if (v == V) { - backtracking::printSolution(color); - return; - } - - // Consider this vertex v and try different colors - for (int c = 1; c <= m; c++) { - // Check if assignment of color c to v is fine - if (backtracking::isSafe(v, graph, color, c)) { - color[v] = c; - - // recur to assign colors to rest of the vertices - backtracking::graphColoring(graph, m, color, v + 1); - - // If assigning color c doesn't lead to a solution then remove it - color[v] = 0; - } +/** + * @brief Utility function to check if the current color assignment is safe for + * vertex v + * @tparam V number of vertices in the graph + * @param v index of graph vertex to check + * @param graph matrix of graph nonnectivity + * @param color vector of colors assigned to the graph nodes/vertices + * @param c color value to check for the node `v` + * @returns `true` if the color is safe to be assigned to the node + * @returns `false` if the color is not safe to be assigned to the node + */ +template +bool isSafe(int v, const std::array, V>& graph, const std::array & color, int c) { + for (int i = 0; i < V; i++) { + if (graph[v][i] && c == color[i]) { + return false; } } + return true; +} + +/** + * @brief Recursive utility function to solve m coloring problem + * @tparam V number of vertices in the graph + * @param graph matrix of graph nonnectivity + * @param m number of colors + * @param [in,out] color description // used in,out to notify in documentation + * that this parameter gets modified by the function + * @param v index of graph vertex to check + */ +template +void graphColoring(const std::array, V>& graph, int m, std::array color, int v) { + // base case: + // If all vertices are assigned a color then return true + if (v == V) { + printSolution(color); + return; + } + + // Consider this vertex v and try different colors + for (int c = 1; c <= m; c++) { + // Check if assignment of color c to v is fine + if (isSafe(v, graph, color, c)) { + color[v] = c; + + // recur to assign colors to rest of the vertices + graphColoring(graph, m, color, v + 1); + + // If assigning color c doesn't lead to a solution then remove it + color[v] = 0; + } + } +} +} // namespace graph_coloring } // namespace backtracking /** - * Main function + * @brief Main function + * @returns 0 on exit */ int main() { // Create following graph and test whether it is 3 colorable @@ -112,6 +122,6 @@ int main() { int m = 3; // Number of colors std::array color{}; - backtracking::graphColoring(graph, m, color, 0); + backtracking::graph_coloring::graphColoring(graph, m, color, 0); return 0; } diff --git a/backtracking/knight_tour.cpp b/backtracking/knight_tour.cpp index 88883ee07..c919bc088 100644 --- a/backtracking/knight_tour.cpp +++ b/backtracking/knight_tour.cpp @@ -12,70 +12,77 @@ * @author [Nikhil Arora](https://github.com/nikhilarora068) * @author [David Leal](https://github.com/Panquesito7) */ -#include -#include +#include /// for IO operations +#include /// for std::array /** * @namespace backtracking * @brief Backtracking algorithms */ namespace backtracking { - /** - * A utility function to check if i,j are valid indexes for N*N chessboard - * @tparam V number of vertices in array - * @param x current index in rows - * @param y current index in columns - * @param sol matrix where numbers are saved - * @returns `true` if .... - * @returns `false` if .... - */ - template - bool issafe(int x, int y, const std::array , V>& sol) { - return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1); - } - - /** - * Knight's tour algorithm - * @tparam V number of vertices in array - * @param x current index in rows - * @param y current index in columns - * @param mov movement to be done - * @param sol matrix where numbers are saved - * @param xmov next move of knight (x coordinate) - * @param ymov next move of knight (y coordinate) - * @returns `true` if solution exists - * @returns `false` if solution does not exist - */ - template - bool solve(int x, int y, int mov, std::array , V> &sol, - const std::array &xmov, std::array &ymov) { - int k, xnext, ynext; - - if (mov == V * V) { - return true; - } - - for (k = 0; k < V; k++) { - xnext = x + xmov[k]; - ynext = y + ymov[k]; - - if (backtracking::issafe(xnext, ynext, sol)) { - sol[xnext][ynext] = mov; - - if (backtracking::solve(xnext, ynext, mov + 1, sol, xmov, ymov) == true) { - return true; - } - else { - sol[xnext][ynext] = -1; - } - } - } - return false; - } -} // namespace backtracking +/** + * @namespace knight_tour + * @brief Functions for the [Knight's tour](https://en.wikipedia.org/wiki/Knight%27s_tour) algorithm + */ +namespace knight_tour { +/** + * A utility function to check if i,j are valid indexes for N*N chessboard + * @tparam V number of vertices in array + * @param x current index in rows + * @param y current index in columns + * @param sol matrix where numbers are saved + * @returns `true` if .... + * @returns `false` if .... + */ +template +bool issafe(int x, int y, const std::array , V>& sol) { + return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1); +} /** - * Main function + * Knight's tour algorithm + * @tparam V number of vertices in array + * @param x current index in rows + * @param y current index in columns + * @param mov movement to be done + * @param sol matrix where numbers are saved + * @param xmov next move of knight (x coordinate) + * @param ymov next move of knight (y coordinate) + * @returns `true` if solution exists + * @returns `false` if solution does not exist + */ +template +bool solve(int x, int y, int mov, std::array , V> &sol, + const std::array &xmov, std::array &ymov) { + int k = 0, xnext = 0, ynext = 0; + + if (mov == V * V) { + return true; + } + + for (k = 0; k < V; k++) { + xnext = x + xmov[k]; + ynext = y + ymov[k]; + + if (issafe(xnext, ynext, sol)) { + sol[xnext][ynext] = mov; + + if (solve(xnext, ynext, mov + 1, sol, xmov, ymov) == true) { + return true; + } + else { + sol[xnext][ynext] = -1; + } + } + } + return false; +} +} // namespace knight_tour +} // namespace backtracking + +/** + * @brief Main function + * @returns 0 on exit */ int main() { const int n = 8; @@ -91,7 +98,7 @@ int main() { sol[0][0] = 0; - bool flag = backtracking::solve(0, 0, 1, sol, xmov, ymov); + bool flag = backtracking::knight_tour::solve(0, 0, 1, sol, xmov, ymov); if (flag == false) { std::cout << "Error: Solution does not exist\n"; } diff --git a/backtracking/minimax.cpp b/backtracking/minimax.cpp index c39018805..c461da4cd 100644 --- a/backtracking/minimax.cpp +++ b/backtracking/minimax.cpp @@ -15,10 +15,10 @@ * @author [Gleison Batista](https://github.com/gleisonbs) * @author [David Leal](https://github.com/Panquesito7) */ -#include -#include -#include -#include +#include /// for std::max, std::min +#include /// for log2 +#include /// for IO operations +#include /// for std::array /** * @namespace backtracking @@ -26,13 +26,13 @@ */ namespace backtracking { /** - * Check which number is the maximum/minimum in the array + * @brief Check which is the maximum/minimum number in the array * @param depth current depth in game tree * @param node_index current index in array * @param is_max if current index is the longest number * @param scores saved numbers in array * @param height maximum height for game tree - * @return maximum or minimum number + * @returns the maximum or minimum number */ template int minimax(int depth, int node_index, bool is_max, @@ -46,10 +46,11 @@ int minimax(int depth, int node_index, bool is_max, return is_max ? std::max(v1, v2) : std::min(v1, v2); } -} // namespace backtracking +} // namespace backtracking /** - * Main function + * @brief Main function + * @returns 0 on exit */ int main() { std::array scores = {90, 23, 6, 33, 21, 65, 123, 34423}; diff --git a/backtracking/n_queens.cpp b/backtracking/n_queens.cpp index 89d907750..451f6dc23 100644 --- a/backtracking/n_queens.cpp +++ b/backtracking/n_queens.cpp @@ -23,97 +23,98 @@ * @brief Backtracking algorithms */ namespace backtracking { - /** - * @namespace n_queens - * @brief Functions for [Eight Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle. - */ - namespace n_queens { - /** - * Utility function to print matrix - * @tparam n number of matrix size - * @param board matrix where numbers are saved - */ - template - void printSolution(const std::array, n> &board) { +/** + * @namespace n_queens + * @brief Functions for [Eight Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle. + */ +namespace n_queens { +/** + * Utility function to print matrix + * @tparam n number of matrix size + * @param board matrix where numbers are saved + */ +template +void printSolution(const std::array, n> &board) { + std::cout << "\n"; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + std::cout << "" << board[i][j] << " "; + } std::cout << "\n"; - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - std::cout << "" << board[i][j] << " "; - } - std::cout << "\n"; - } } + } - /** - * Check if a queen can be placed on matrix - * @tparam n number of matrix size - * @param board matrix where numbers are saved - * @param row current index in rows - * @param col current index in columns - * @returns `true` if queen can be placed on matrix - * @returns `false` if queen can't be placed on matrix - */ - template - bool isSafe(const std::array, n> &board, const int &row, - const int &col) { - int i = 0, j = 0; +/** + * Check if a queen can be placed on matrix + * @tparam n number of matrix size + * @param board matrix where numbers are saved + * @param row current index in rows + * @param col current index in columns + * @returns `true` if queen can be placed on matrix + * @returns `false` if queen can't be placed on matrix + */ +template +bool isSafe(const std::array, n> &board, const int &row, + const int &col) { + int i = 0, j = 0; - // Check this row on left side - for (i = 0; i < col; i++) { - if (board[row][i]) { - return false; - } - } - - // Check upper diagonal on left side - for (i = row, j = col; i >= 0 && j >= 0; i--, j--) { - if (board[i][j]) { - return false; - } - } - // Check lower diagonal on left side - for (i = row, j = col; j >= 0 && i < n; i++, j--) { - if (board[i][j]) { - return false; - } - } - return true; + // Check this row on left side + for (i = 0; i < col; i++) { + if (board[row][i]) { + return false; } + } - /** - * Solve n queens problem - * @tparam n number of matrix size - * @param board matrix where numbers are saved - * @param col current index in columns - */ - template - void solveNQ(std::array, n> board, const int &col) { - if (col >= n) { - printSolution(board); - return; - } - - // Consider this column and try placing - // this queen in all rows one by one - for (int i = 0; i < n; i++) { - // Check if queen can be placed - // on board[i][col] - if (isSafe(board, i, col)) { - // Place this queen in matrix - board[i][col] = 1; - - // Recursive to place rest of the queens - solveNQ(board, col + 1); - - board[i][col] = 0; // backtrack - } - } + // Check upper diagonal on left side + for (i = row, j = col; i >= 0 && j >= 0; i--, j--) { + if (board[i][j]) { + return false; } - } // namespace n_queens + } + // Check lower diagonal on left side + for (i = row, j = col; j >= 0 && i < n; i++, j--) { + if (board[i][j]) { + return false; + } + } + return true; +} + +/** + * Solve n queens problem + * @tparam n number of matrix size + * @param board matrix where numbers are saved + * @param col current index in columns + */ +template +void solveNQ(std::array, n> board, const int &col) { + if (col >= n) { + printSolution(board); + return; + } + + // Consider this column and try placing + // this queen in all rows one by one + for (int i = 0; i < n; i++) { + // Check if queen can be placed + // on board[i][col] + if (isSafe(board, i, col)) { + // Place this queen in matrix + board[i][col] = 1; + + // Recursive to place rest of the queens + solveNQ(board, col + 1); + + board[i][col] = 0; // backtrack + } + } +} +} // namespace n_queens } // namespace backtracking /** - * Main function + * @brief Main function + * @returns 0 on exit */ int main() { const int n = 4; diff --git a/backtracking/n_queens_all_solution_optimised.cpp b/backtracking/n_queens_all_solution_optimised.cpp index bd150a98d..525d4c2db 100644 --- a/backtracking/n_queens_all_solution_optimised.cpp +++ b/backtracking/n_queens_all_solution_optimised.cpp @@ -111,7 +111,7 @@ int main() { std::array, n> board{}; if (n % 2 == 0) { - for (int i = 0; i <= n / 2 - 1; i++) { // 😎 + for (int i = 0; i <= n / 2 - 1; i++) { if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) { board[i][0] = 1; backtracking::n_queens_optimized::NQueenSol(board, 1); @@ -119,7 +119,7 @@ int main() { } } } else { - for (int i = 0; i <= n / 2; i++) { // 😏 + for (int i = 0; i <= n / 2; i++) { if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) { board[i][0] = 1; backtracking::n_queens_optimized::NQueenSol(board, 1); diff --git a/backtracking/nqueen_print_all_solutions.cpp b/backtracking/nqueen_print_all_solutions.cpp index 5aa5c71a2..df99f2a33 100644 --- a/backtracking/nqueen_print_all_solutions.cpp +++ b/backtracking/nqueen_print_all_solutions.cpp @@ -7,8 +7,8 @@ * @author [David Leal](https://github.com/Panquesito7) * */ -#include -#include +#include /// for IO operations +#include /// for std::array /** * @namespace backtracking @@ -17,12 +17,12 @@ namespace backtracking { /** * @namespace n_queens_all_solutions - * @brief Functions for [Eight + * @brief Functions for the [Eight * Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle with all solutions. */ namespace n_queens_all_solutions { /** - * Utility function to print matrix + * @brief Utility function to print matrix * @tparam n number of matrix size * @param board matrix where numbers are saved */ @@ -38,7 +38,7 @@ void PrintSol(const std::array, n>& board) { } /** - * Check if a queen can be placed on matrix + * @brief Check if a queen can be placed on the matrix * @tparam n number of matrix size * @param board matrix where numbers are saved * @param row current index in rows @@ -70,7 +70,7 @@ bool CanIMove(const std::array, n>& board, int row, int col) } /** - * Solve n queens problem + * @brief Main function to solve the N Queens problem * @tparam n number of matrix size * @param board matrix where numbers are saved * @param col current index in columns @@ -89,11 +89,12 @@ void NQueenSol(std::array, n> board, int col) { } } } -} // namespace n_queens_all_solutions +} // namespace n_queens_all_solutions } // namespace backtracking /** - * Main function + * @brief Main function + * @returns 0 on exit */ int main() { const int n = 4; diff --git a/backtracking/rat_maze.cpp b/backtracking/rat_maze.cpp index 6dfda965c..7ac77b1d5 100644 --- a/backtracking/rat_maze.cpp +++ b/backtracking/rat_maze.cpp @@ -16,9 +16,9 @@ * @author [David Leal](https://github.com/Panquesito7) */ -#include -#include -#include +#include /// for std::array +#include /// for IO operations +#include /// for assert /** * @namespace backtracking @@ -39,7 +39,8 @@ namespace rat_maze { * @param currposcol current position in columns * @param maze matrix where numbers are saved * @param soln matrix to problem solution - * @returns 0 on end + * @returns `true` if there exists a solution to move one step ahead in a column or in a row + * @returns `false` for the backtracking part */ template bool solveMaze(int currposrow, int currposcol, @@ -78,7 +79,7 @@ bool solveMaze(int currposrow, int currposcol, } // namespace backtracking /** - * @brief Test implementations + * @brief Self-test implementations * @returns void */ static void test(){ @@ -96,8 +97,8 @@ static void test(){ } } - int currposrow = 0; // Current position in rows - int currposcol = 0; // Current position in columns + int currposrow = 0; // Current position in the rows + int currposcol = 0; // Current position in the columns assert(backtracking::rat_maze::solveMaze(currposrow, currposcol, maze, soln) == 1); @@ -108,6 +109,6 @@ static void test(){ * @returns 0 on exit */ int main() { - test(); // run the tests + test(); // run self-test implementations return 0; } diff --git a/backtracking/sudoku_solve.cpp b/backtracking/sudoku_solve.cpp index d631d5dfd..b7b1f9ab9 100644 --- a/backtracking/sudoku_solve.cpp +++ b/backtracking/sudoku_solve.cpp @@ -13,126 +13,133 @@ * @author [DarthCoder3200](https://github.com/DarthCoder3200) * @author [David Leal](https://github.com/Panquesito7) */ -#include -#include +#include /// for IO operations +#include /// for assert /** * @namespace backtracking * @brief Backtracking algorithms */ namespace backtracking { - /** - * Checks if it's possible to place a number 'no' - * @tparam V number of vertices in the array - * @param mat matrix where numbers are saved - * @param i current index in rows - * @param j current index in columns - * @param no number to be added in matrix - * @param n number of times loop will run - * @returns `true` if 'mat' is different from 'no' - * @returns `false` if 'mat' equals to 'no' - */ - template - bool isPossible(const std::array , V> &mat, int i, int j, int no, int n) { - /// 'no' shouldn't be present in either row i or column j - for (int x = 0; x < n; x++) { - if (mat[x][j] == no || mat[i][x] == no) { +/** + * @namespace sudoku_solver + * @brief Functions for the [Sudoku Solver](https://en.wikipedia.org/wiki/Sudoku) implementation + */ +namespace sudoku_solver { +/** + * @brief Check if it's possible to place a number (`no` parameter) + * @tparam V number of vertices in the array + * @param mat matrix where numbers are saved + * @param i current index in rows + * @param j current index in columns + * @param no number to be added in matrix + * @param n number of times loop will run + * @returns `true` if 'mat' is different from 'no' + * @returns `false` if 'mat' equals to 'no' + */ +template +bool isPossible(const std::array , V> &mat, int i, int j, int no, int n) { + /// `no` shouldn't be present in either row i or column j + for (int x = 0; x < n; x++) { + if (mat[x][j] == no || mat[i][x] == no) { + return false; + } + } + + /// `no` shouldn't be present in the 3*3 subgrid + int sx = (i / 3) * 3; + int sy = (j / 3) * 3; + + for (int x = sx; x < sx + 3; x++) { + for (int y = sy; y < sy + 3; y++) { + if (mat[x][y] == no) { return false; } } + } - /// 'no' shouldn't be present in the 3*3 subgrid - int sx = (i / 3) * 3; - int sy = (j / 3) * 3; - - for (int x = sx; x < sx + 3; x++) { - for (int y = sy; y < sy + 3; y++) { - if (mat[x][y] == no) { - return false; - } + return true; +} +/** + * @brief Utility function to print the matrix + * @tparam V number of vertices in array + * @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 + * @return void + */ +template +void printMat(const std::array , V> &mat, const std::array , V> &starting_mat, int n) { + for (int i = 0; i < n; i++) { + 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] << " "; + } + if ((j + 1) % 3 == 0) { + std::cout << '\t'; } } - - return true; - } - /** - * Utility function to print matrix - * @tparam V number of vertices in array - * @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 - * @return void - */ - template - void printMat(const std::array , V> &mat, const std::array , V> &starting_mat, int n) { - for (int i = 0; i < n; i++) { - 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] << " "; - } - if ((j + 1) % 3 == 0) { - std::cout << '\t'; - } - } - if ((i + 1) % 3 == 0) { - std::cout << std::endl; - } + if ((i + 1) % 3 == 0) { std::cout << std::endl; } + std::cout << std::endl; } - - /** - * Sudoku algorithm - * @tparam V number of vertices in array - * @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 j current index in columns - * @returns `true` if 'no' was placed - * @returns `false` if 'no' was not placed - */ - template - bool solveSudoku(std::array , V> &mat, const std::array , V> &starting_mat, int i, int j) { - /// Base Case - if (i == 9) { - /// Solved for 9 rows already - backtracking::printMat(mat, starting_mat, 9); - return true; - } - - /// Crossed the last Cell in the row - if (j == 9) { - return backtracking::solveSudoku(mat, starting_mat, i + 1, 0); - } - - /// Blue Cell - Skip - if (mat[i][j] != 0) { - return backtracking::solveSudoku(mat, starting_mat, i, j + 1); - } - /// White Cell - /// Try to place every possible no - for (int no = 1; no <= 9; no++) { - if (backtracking::isPossible(mat, i, j, no, 9)) { - /// Place the 'no' - assuming a solution will exist - mat[i][j] = no; - bool solution_found = backtracking::solveSudoku(mat, starting_mat, i, j + 1); - if (solution_found) { - return true; - } - /// Couldn't find a solution - /// loop will place the next no. - } - } - /// Solution couldn't be found for any of the numbers provided - mat[i][j] = 0; - return false; - } -} // namespace backtracking +} /** - * Main function + * @brief Main function to implement the Sudoku algorithm + * @tparam V number of vertices in array + * @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 j current index in columns + * @returns `true` if 'no' was placed + * @returns `false` if 'no' was not placed + */ +template +bool solveSudoku(std::array , V> &mat, const std::array , V> &starting_mat, int i, int j) { + /// Base Case + if (i == 9) { + /// Solved for 9 rows already + printMat(mat, starting_mat, 9); + return true; + } + + /// Crossed the last Cell in the row + if (j == 9) { + return solveSudoku(mat, starting_mat, i + 1, 0); + } + + /// Blue Cell - Skip + if (mat[i][j] != 0) { + return solveSudoku(mat, starting_mat, i, j + 1); + } + /// White Cell + /// Try to place every possible no + for (int no = 1; no <= 9; no++) { + if (isPossible(mat, i, j, no, 9)) { + /// Place the 'no' - assuming a solution will exist + mat[i][j] = no; + bool solution_found = solveSudoku(mat, starting_mat, i, j + 1); + if (solution_found) { + return true; + } + /// Couldn't find a solution + /// loop will place the next `no`. + } + } + /// Solution couldn't be found for any of the numbers provided + mat[i][j] = 0; + return false; +} +} // namespace sudoku_solver +} // namespace backtracking + +/** + * @brief Main function + * @returns 0 on exit */ int main() { const int V = 9; @@ -148,10 +155,10 @@ int main() { std::array {0, 0, 0, 0, 8, 0, 0, 7, 9} }; - backtracking::printMat(mat, mat, 9); + backtracking::sudoku_solver::printMat(mat, mat, 9); std::cout << "Solution " << std::endl; std::array , V> starting_mat = mat; - backtracking::solveSudoku(mat, starting_mat, 0, 0); + backtracking::sudoku_solver::solveSudoku(mat, starting_mat, 0, 0); return 0; } diff --git a/math/check_prime.cpp b/math/check_prime.cpp index ea4e6d52d..79705e7f0 100644 --- a/math/check_prime.cpp +++ b/math/check_prime.cpp @@ -22,11 +22,11 @@ template bool is_prime(T num) { bool result = true; if (num <= 1) { - return 0; + return false; } else if (num == 2) { - return 1; + return true; } else if ((num & 1) == 0) { - return 0; + return false; } if (num >= 3) { for (T i = 3; (i * i) < (num); i = (i + 2)) { diff --git a/others/iterative_tree_traversals.cpp b/others/iterative_tree_traversals.cpp index 6d0e7dc2e..0200e1207 100644 --- a/others/iterative_tree_traversals.cpp +++ b/others/iterative_tree_traversals.cpp @@ -204,8 +204,9 @@ static void test2(others::iterative_tree_traversals::BinaryTree binaryTree, othe result = binaryTree.postOrderIterative(root); // Self-testing the result using `assert` - for(int i = 0; i < result.size(); i++) + for(int i = 0; i < result.size(); i++) { assert(actual_result[i] == result[i]); + } // Printing the result storing postorder. std::cout<< "\nPostOrder Traversal Is : "<< std::endl; @@ -228,8 +229,9 @@ static void test3(others::iterative_tree_traversals::BinaryTree binaryTree, othe result = binaryTree.inOrderIterative(root); // Self-testing the result using `assert` - for(int i = 0; i < result.size(); i++) + for(int i = 0; i < result.size(); i++) { assert(actual_result[i] == result[i]); + } // Printing the result storing inorder. std::cout<< "\nInOrder Traversal Is : "<< std::endl; @@ -252,8 +254,9 @@ static void test4(others::iterative_tree_traversals::BinaryTree binaryTree, othe result = binaryTree.preOrderIterative(root); // Self-testing the result using `assert` - for(int i = 0; i < result.size(); i++) + for(int i = 0; i < result.size(); i++) { assert(actual_result[i] == result[i]); + } // Printing the result storing preorder. std::cout<< "\nPreOrder Traversal Is : "<< std::endl; @@ -276,8 +279,9 @@ static void test5(others::iterative_tree_traversals::BinaryTree binaryTree, othe result = binaryTree.postOrderIterative(root); // Self-testing the result using `assert` - for(int i = 0; i < result.size(); i++) + for(int i = 0; i < result.size(); i++) { assert(actual_result[i] == result[i]); + } // Printing the result storing postorder. std::cout<< "\nPostOrder Traversal Is : "<< std::endl; @@ -300,8 +304,9 @@ static void test6(others::iterative_tree_traversals::BinaryTree binaryTree, othe result = binaryTree.inOrderIterative(root); // Self-testing the result using `assert` - for(int i = 0; i < result.size(); i++) + for(int i = 0; i < result.size(); i++) { assert(actual_result[i] == result[i]); + } // Printing the result storing inorder. std::cout<< "\nInOrder Traversal Is : "<< std::endl;