-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheight_queens.cpp
79 lines (75 loc) · 2.12 KB
/
eight_queens.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
using namespace std;
class Chess {
private:
int* chess; // chess is a one-dimensional array.
int chessSize; // Size of the chess, for example, 8.
int queenCount; // Number of queens by now.
int solutionNum; // Total number of solutions.
public:
Chess() = delete; // No default constructor.
Chess(int chessSize) : chessSize(chessSize), queenCount(0), solutionNum(0) {
chess = new int[chessSize];
for (int i = 0; i < size(); i++) {
chess[i] = -1; // Means this row has not been set yet.
}
}
~Chess() { delete[] chess; }
int size() const { return chessSize; }
int queens() const { return queenCount; }
int solutions() const { return solutionNum; }
void solve() {
try_queen(0); // Try put a queen from row 0.
}
private:
/* IMPORTANT FUNCTION IS TRY_QUEENS */
void try_queen(int row) {
if (queens() == size()) {
print(); // Find one solution, print it out.
++solutionNum;
} else {
for (int col = 0; col < size(); col++) {
chess[row] = col;
if (check_valid(row)) {
++queenCount;
try_queen(row + 1);
--queenCount;
}
}
}
}
bool check_valid(int row) {
int col = chess[row];
/* Check columns */
for (int i = 0; i < row; i++) {
if (chess[i] == col) {
return false;
}
}
/* Check diagonal */
for (int i = 0; i < row; i++) {
if (i + chess[i] == row + col) {
return false;
}
if (i - chess[i] == row - col) {
return false;
}
}
return true;
}
void print() const {
for (int i = 0; i < size(); i++) {
cout << chess[i] << " ";
}
cout << endl;
}
};
int main() {
int n;
cin >> n;
Chess ch(n);
ch.solve();
cout << "Number of solutions: " << ch.solutions() << endl;
system("pause");
return 0;
}