2020-08-27 01:57:21 +08:00
|
|
|
/**
|
|
|
|
* @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)
|
|
|
|
*
|
|
|
|
*/
|
2019-12-27 19:27:24 +08:00
|
|
|
#include <iostream>
|
2020-08-27 01:57:21 +08:00
|
|
|
#include <array>
|
2019-12-27 19:27:24 +08:00
|
|
|
|
2020-08-27 01:57:21 +08:00
|
|
|
/**
|
|
|
|
* @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) {
|
2019-12-27 19:27:24 +08:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
for (int j = 0; j < n; j++) {
|
2020-08-27 01:57:21 +08:00
|
|
|
std::cout << board[i][j] << " ";
|
2019-12-27 19:27:24 +08:00
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
|
2020-08-27 01:57:21 +08:00
|
|
|
/**
|
|
|
|
* 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) {
|
2019-12-27 19:27:24 +08:00
|
|
|
/// check in the row
|
|
|
|
for (int i = 0; i < col; i++) {
|
2020-08-27 01:57:21 +08:00
|
|
|
if (board[row][i] == 1) {
|
2019-12-27 19:27:24 +08:00
|
|
|
return false;
|
2020-08-27 01:57:21 +08:00
|
|
|
}
|
2019-12-27 19:27:24 +08:00
|
|
|
}
|
|
|
|
/// check the first diagonal
|
|
|
|
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
|
2020-08-27 01:57:21 +08:00
|
|
|
if (board[i][j] == 1) {
|
2019-12-27 19:27:24 +08:00
|
|
|
return false;
|
2020-08-27 01:57:21 +08:00
|
|
|
}
|
2019-12-27 19:27:24 +08:00
|
|
|
}
|
|
|
|
/// check the second diagonal
|
|
|
|
for (int i = row, j = col; i <= n - 1 && j >= 0; i++, j--) {
|
2020-08-27 01:57:21 +08:00
|
|
|
if (board[i][j] == 1) {
|
2019-12-27 19:27:24 +08:00
|
|
|
return false;
|
2020-08-27 01:57:21 +08:00
|
|
|
}
|
2019-12-27 19:27:24 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-27 01:57:21 +08:00
|
|
|
/**
|
|
|
|
* 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) {
|
2019-12-27 19:27:24 +08:00
|
|
|
if (col >= n) {
|
2020-08-27 01:57:21 +08:00
|
|
|
PrintSol(board);
|
2019-12-27 19:27:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < n; i++) {
|
2020-08-27 01:57:21 +08:00
|
|
|
if (CanIMove(board, i, col)) {
|
|
|
|
board[i][col] = 1;
|
|
|
|
NQueenSol(board, col + 1);
|
|
|
|
board[i][col] = 0;
|
2019-12-27 19:27:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 01:57:21 +08:00
|
|
|
} // namespace n_queens_all_solutions
|
|
|
|
} // namespace backtracking
|
2019-12-27 19:27:24 +08:00
|
|
|
|
2020-08-27 01:57:21 +08:00
|
|
|
/**
|
|
|
|
* Main function
|
|
|
|
*/
|
2019-12-27 19:27:24 +08:00
|
|
|
int main() {
|
2020-08-27 01:57:21 +08:00
|
|
|
const int n = 4;
|
|
|
|
std::array<std::array<int, n>, n> board{0};
|
|
|
|
|
|
|
|
backtracking::n_queens_all_solutions::NQueenSol(board, 0);
|
2019-12-27 19:27:24 +08:00
|
|
|
}
|