Threads
n_queens_all_so ( 16879 ) - n_queens_all_so ( 16879 ) stack: main(backtracking/n_queens_all_solution_optimised.cpp:109)
/**
* @file
* @brief [N queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) all
* optimized
*
* @author [Sombit Bose](https://github.com/deadshotsb)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include
#include
/**
* @namespace backtracking
* @brief Backtracking algorithms
*/
namespace backtracking {
/**
* @namespace n_queens_optimized
* @brief Functions for [Eight
* Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle optimized.
*/
namespace n_queens_optimized {
/**
* 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 << std::endl;
}
std::cout << std::endl;
if (n % 2 == 0 || (n % 2 == 1 && board[n / 2 + 1][0] != 1)) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cout << board[j][i] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
}
/**
* 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) {
return false;
}
}
/// check the first diagonal
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
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) {
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 NQueenSol(std::array, n> board, int col) {
if (col >= n) {
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;
}
}
}
} // namespace n_queens_optimized
} // namespace backtracking
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
const int n = 4;
std::array, n> board{};
if (n % 2 == 0) {
for (int i = 0; i <= n / 2 - 1; i++) {
if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) {
board[i][0] = 1;
backtracking::n_queens_optimized::NQueenSol(board, 1);
board[i][0] = 0;
}
}
} else {
for (int i = 0; i <= n / 2; i++) {
if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) {
board[i][0] = 1;
backtracking::n_queens_optimized::NQueenSol(board, 1);
board[i][0] = 0;
}
}
}
return 0;
}
Variables All
No. | From | Name | Value |
---|---|---|---|
NA | NA | NA | NA |
Process | Thread Filter |
---|---|
16879 n_queens_all_so | 16879 n_queens_all_so |
No. | PN | PID | TID | TN | Message |
---|---|---|---|---|---|
1 | n_queens_all_so | 16879 | 16879 | n_queens_all_so | 0 0 1 0 |
2 | n_queens_all_so | 16879 | 16879 | n_queens_all_so | 1 0 0 0 |
3 | n_queens_all_so | 16879 | 16879 | n_queens_all_so | 0 0 0 1 |
4 | n_queens_all_so | 16879 | 16879 | n_queens_all_so | 0 1 0 0 |
5 | n_queens_all_so | 16879 | 16879 | n_queens_all_so | |
6 | n_queens_all_so | 16879 | 16879 | n_queens_all_so | 0 1 0 0 |
7 | n_queens_all_so | 16879 | 16879 | n_queens_all_so | 0 0 0 1 |
8 | n_queens_all_so | 16879 | 16879 | n_queens_all_so | 1 0 0 0 |
9 | n_queens_all_so | 16879 | 16879 | n_queens_all_so | 0 0 1 0 |
10 | n_queens_all_so | 16879 | 16879 | n_queens_all_so | |
END | 0 | 0 | 0 | 0 | 0 |
×
Commands and Shortcuts
No. | Command | Shortcuts | Description |
---|---|---|---|
1 | GB | Alt + LEFT, Alt + A | Go Backward |
2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
3 | PPE | Alt + UP, Alt + W | Previous Process End |
4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
9 | TB | LEFT, A | current Thread Backward |
10 | TF | RIGHT, D | current Thread Foreward |
11 | LU | UP, W | go Line Up of current code block in current thread |
12 | LD | DOWN, S | go Line Down of current code block in current thread |
13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
15 | BS | Home | go to code Block Start |
16 | BE | End | go to code Block End |
Project: | algorithms-cpp |
Update: | 20240212 |
Commit: | 2dadbf73f |
Source Code: | backtracking.n_queens_all_solution_optimised |
BuildTool: | cpp17 |
Compiler: | cpp17 |
Runtime: | Openjdk17 |
System: | MySystemD |
Kernel: | Linux5.10.211 |
Cpu: | Intel:Corei7-7700K |
Machine: | AwesomeMachine |