mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
[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>
This commit is contained in:
parent
44f4f4d6c3
commit
f0c218c789
@ -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 <iostream>
|
||||
#define n 4
|
||||
#include <array>
|
||||
|
||||
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 <size_t n>
|
||||
void PrintSol(const std::array<std::array<int, n>, 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 <size_t n>
|
||||
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)
|
||||
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 <size_t n>
|
||||
void NQueenSol(std::array<std::array<int, n>, 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<std::array<int, n>, n> board{0};
|
||||
|
||||
backtracking::n_queens_all_solutions::NQueenSol(board, 0);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user