From f4c187b9aeb8b379341561e87fef100322582a78 Mon Sep 17 00:00:00 2001 From: David Leal Date: Sun, 9 Aug 2020 14:00:54 -0500 Subject: [PATCH 1/6] [fix/docs]: Improve backtracking/n_queens.cpp --- backtracking/n_queens.cpp | 156 ++++++++++++++++++++++++++------------ 1 file changed, 108 insertions(+), 48 deletions(-) diff --git a/backtracking/n_queens.cpp b/backtracking/n_queens.cpp index 8aab5df7b..6da8d8679 100644 --- a/backtracking/n_queens.cpp +++ b/backtracking/n_queens.cpp @@ -1,63 +1,123 @@ +/** + * @file n_queens.cpp + * @brief [Eight Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) + * puzzle + * + * @details + * The **eight queens puzzle** is the problem of placing eight chess queens on + * an 8×8 chessboard so that no two queens threaten each other; thus, a solution + * requires that no two queens share the same row, column, or diagonal. The + * eight queens puzzle is an example of the more general **n queens problem** of + * placing n non-attacking queens on an n×n chessboard, for which solutions + * exist for all natural numbers n with the exception of n = 2 and n = 3. + * + * @author Unknown author + * @author [David Leal](https://github.com/Panquesito7) + * + */ #include -#define N 4 -using namespace std; +#include -void printSolution(int board[N][N]) { - cout << "\n"; - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) cout << "" << board[i][j]; - cout << "\n"; +/** + * @namespace backtracking + * @brief Backtracking algorithms + */ +namespace backtracking { +/** + * 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"; + } } -bool isSafe(int board[N][N], int row, int col) { - int i, j; +/** + * 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 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 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; } -void solveNQ(int board[N][N], 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 board[i][col] */ - // cout<<"\n"< +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; + + // Recur to place rest of the queens + solveNQ(board, col + 1); + + board[i][col] = 0; // backtrack } + } } +} // namespace backtracking +/** + * Main function + */ int main() { - int board[N][N] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; + const int n = 4; + std::array, n> board = { + std::array({0, 0, 0, 0}), + std::array({0, 0, 0, 0}), + std::array({0, 0, 0, 0}), + std::array({0, 0, 0, 0}) + }; - solveNQ(board, 0); - return 0; + backtracking::solveNQ(board, 0); + return 0; } From de5f695afadbe58d0f0015fc9030b9921f55abf1 Mon Sep 17 00:00:00 2001 From: David Leal Date: Mon, 10 Aug 2020 12:37:06 -0500 Subject: [PATCH 2/6] fix: Add spaces between numbers --- backtracking/n_queens.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/n_queens.cpp b/backtracking/n_queens.cpp index 6da8d8679..d879795ee 100644 --- a/backtracking/n_queens.cpp +++ b/backtracking/n_queens.cpp @@ -33,7 +33,7 @@ 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 << "" << board[i][j] << " "; } std::cout << "\n"; } From 358f56f9bea33093aa767631f23a90b63098b052 Mon Sep 17 00:00:00 2001 From: David Leal Date: Mon, 10 Aug 2020 12:37:51 -0500 Subject: [PATCH 3/6] docs: Minor correction --- backtracking/n_queens.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/n_queens.cpp b/backtracking/n_queens.cpp index d879795ee..c864fff6a 100644 --- a/backtracking/n_queens.cpp +++ b/backtracking/n_queens.cpp @@ -97,7 +97,7 @@ void solveNQ(std::array, n> board, const int &col) { // Place this queen in matrix board[i][col] = 1; - // Recur to place rest of the queens + // Recursive to place rest of the queens solveNQ(board, col + 1); board[i][col] = 0; // backtrack From b7621157cb978c5b5e3761bddf6f8ffbc6269216 Mon Sep 17 00:00:00 2001 From: David Leal Date: Mon, 10 Aug 2020 18:37:51 -0500 Subject: [PATCH 4/6] fix: Add namespace member: n_queens --- backtracking/n_queens.cpp | 156 ++++++++++++++++++++------------------ 1 file changed, 81 insertions(+), 75 deletions(-) diff --git a/backtracking/n_queens.cpp b/backtracking/n_queens.cpp index c864fff6a..7ba7cf52e 100644 --- a/backtracking/n_queens.cpp +++ b/backtracking/n_queens.cpp @@ -23,87 +23,93 @@ * @brief Backtracking algorithms */ namespace backtracking { -/** - * 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] << " "; + /** + * @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"; + } } - 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 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 upper diagonal on left side - for (i = row, j = col; i >= 0 && j >= 0; i--, j--) { - if (board[i][j]) { - 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 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 /** From d2c3a3f0d0929399a6d58ea919ce6f856bcac5bd Mon Sep 17 00:00:00 2001 From: David Leal Date: Mon, 10 Aug 2020 18:39:54 -0500 Subject: [PATCH 5/6] fix: lint warnings --- backtracking/n_queens.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/n_queens.cpp b/backtracking/n_queens.cpp index 7ba7cf52e..cefc01451 100644 --- a/backtracking/n_queens.cpp +++ b/backtracking/n_queens.cpp @@ -124,6 +124,6 @@ int main() { std::array({0, 0, 0, 0}) }; - backtracking::solveNQ(board, 0); + backtracking::n_queens::solveNQ(board, 0); return 0; } From 8454057249a29aa4e2d7cf795e22a6e551009043 Mon Sep 17 00:00:00 2001 From: David Leal Date: Thu, 13 Aug 2020 17:03:56 -0500 Subject: [PATCH 6/6] docs: Remove file name --- backtracking/n_queens.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/n_queens.cpp b/backtracking/n_queens.cpp index cefc01451..89d907750 100644 --- a/backtracking/n_queens.cpp +++ b/backtracking/n_queens.cpp @@ -1,5 +1,5 @@ /** - * @file n_queens.cpp + * @file * @brief [Eight Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) * puzzle *