mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-07-16 19:41:16 +08:00
[feat/fix/docs]: Improvements in the backtracking folder (#1553)
* [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.
* clang-format and clang-tidy fixes for 9cc3951d
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com>
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief [Eight Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle)
|
||||
* @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 <iostream>
|
||||
#include <array>
|
||||
#include <array> /// for std::array
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
* @namespace backtracking
|
||||
@@ -17,12 +17,13 @@
|
||||
namespace backtracking {
|
||||
/**
|
||||
* @namespace n_queens_all_solutions
|
||||
* @brief Functions for [Eight
|
||||
* Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle with all solutions.
|
||||
* @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 +39,7 @@ void PrintSol(const std::array<std::array<int, n>, 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
|
||||
@@ -47,7 +48,8 @@ void PrintSol(const std::array<std::array<int, n>, n>& board) {
|
||||
* @returns `false` if queen can't be placed on matrix
|
||||
*/
|
||||
template <size_t n>
|
||||
bool CanIMove(const std::array<std::array<int, n>, n>& board, int row, int col) {
|
||||
bool CanIMove(const std::array<std::array<int, n>, n>& board, int row,
|
||||
int col) {
|
||||
/// check in the row
|
||||
for (int i = 0; i < col; i++) {
|
||||
if (board[row][i] == 1) {
|
||||
@@ -70,7 +72,7 @@ bool CanIMove(const std::array<std::array<int, n>, 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 +91,12 @@ void NQueenSol(std::array<std::array<int, n>, 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;
|
||||
|
||||
Reference in New Issue
Block a user