From f0c218c7899b9c833dd4684dcc099a1ad1acd642 Mon Sep 17 00:00:00 2001 From: David Leal Date: Wed, 26 Aug 2020 12:57:21 -0500 Subject: [PATCH] [fix/docs]: Improve backtracking/nqueen_print_all_solutions.cpp (#1049) * updating DIRECTORY.md * updating DIRECTORY.md * [fix/docs]: Improve backtracking/nqueen_print_all_solutions.cpp Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- backtracking/nqueen_print_all_solutions.cpp | 83 +++++++++++++++++---- 1 file changed, 68 insertions(+), 15 deletions(-) diff --git a/backtracking/nqueen_print_all_solutions.cpp b/backtracking/nqueen_print_all_solutions.cpp index e6736da1e..5aa5c71a2 100644 --- a/backtracking/nqueen_print_all_solutions.cpp +++ b/backtracking/nqueen_print_all_solutions.cpp @@ -1,50 +1,103 @@ +/** + * @file + * @brief [Eight Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) + * puzzle, printing all solutions + * + * @author [Himani Negi](https://github.com/Himani2000) + * @author [David Leal](https://github.com/Panquesito7) + * + */ #include -#define n 4 +#include -void PrintSol(int Board[n][n]) { +/** + * @namespace backtracking + * @brief Backtracking algorithms + */ +namespace backtracking { +/** + * @namespace n_queens_all_solutions + * @brief Functions for [Eight + * Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle with all solutions. + */ +namespace n_queens_all_solutions { +/** + * Utility function to print matrix + * @tparam n number of matrix size + * @param board matrix where numbers are saved + */ +template +void PrintSol(const std::array, n>& board) { 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 << std::endl; } std::cout << std::endl; } -bool CanIMove(int Board[n][n], int row, int col) { +/** + * 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 CanIMove(const std::array, n>& board, int row, int col) { /// check in the row for (int i = 0; i < col; i++) { - if (Board[row][i] == 1) + if (board[row][i] == 1) { return false; + } } /// check the first diagonal for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) { - if (Board[i][j] == 1) + if (board[i][j] == 1) { return false; + } } /// check the second diagonal for (int i = row, j = col; i <= n - 1 && j >= 0; i++, j--) { - if (Board[i][j] == 1) + if (board[i][j] == 1) { return false; + } } return true; } -void NQueenSol(int Board[n][n], int col) { +/** + * 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 NQueenSol(std::array, n> board, int col) { if (col >= n) { - PrintSol(Board); + PrintSol(board); return; } for (int i = 0; i < n; i++) { - if (CanIMove(Board, i, col)) { - Board[i][col] = 1; - NQueenSol(Board, col + 1); - Board[i][col] = 0; + if (CanIMove(board, i, col)) { + board[i][col] = 1; + NQueenSol(board, col + 1); + board[i][col] = 0; } } } +} // namespace n_queens_all_solutions +} // namespace backtracking +/** + * Main function + */ int main() { - int Board[n][n] = {0}; - NQueenSol(Board, 0); + const int n = 4; + std::array, n> board{0}; + + backtracking::n_queens_all_solutions::NQueenSol(board, 0); }