-
Notifications
You must be signed in to change notification settings - Fork 1
/
n_queens.hpp
55 lines (46 loc) · 1.25 KB
/
n_queens.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#if !defined(N_QUEENS)
#define N_QUEENS
#include <bitset>
#include <cstddef>
#include <iostream>
template<std::size_t QUEENS>
struct N_Queens {
std::bitset<QUEENS> board[QUEENS];
int validBoardCount = 0;
void printBoard() const {
std::cout << "Valid Board: " << validBoardCount << '\n';
for (std::bitset<QUEENS> const & row: board) {
std::cout << " " << row << '\n';
}
std::cout << "---\n\n";
}
bool isSafe(int row, std::bitset<QUEENS> const & column) const {
std::bitset<QUEENS> diag_right = column >> 1;
std::bitset<QUEENS> diag_left = column << 1;
for (int check_row = row -1; check_row >= 0; --check_row) {
if ((board[check_row] & (diag_left | column | diag_right)).any()) {
return false;
}
diag_right = diag_right >> 1;
diag_left = diag_left << 1;
}
return true;
}
void addQueen(int row) {
if (row >= QUEENS) {
++validBoardCount;
printBoard();
} else {
std::bitset<QUEENS> column{1};
while(column.any()) {
if (isSafe(row, column)) {
board[row] = column;
addQueen(row + 1);
}
column = column << 1;
}
board[row] = column;
}
}
};
#endif // N_QUEENS