-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnqueens_csp.cpp
68 lines (57 loc) · 1.53 KB
/
nqueens_csp.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
#include <iostream>
#include <vector>
using namespace std;
int solutionCount = 0;
int n;
vector<vector<int>> chessboard(101, vector<int>(101, 0));
void printChessboard() {
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
cout << (chessboard[row][col] ? "Q " : ". ");
}
cout << endl;
}
cout << "-----------------\n";
}
bool isSafe(int row, int col) {
// Check horizontal positions
for (int i = col; i >= 0; i--) {
if (chessboard[row][i]) return false;
}
int i = row, j = col;
// Check the upper left diagonal
while (i >= 0 && j >= 0) {
if (chessboard[i][j]) return false;
i--;
j--;
}
i = row;
j = col;
// Check the lower left diagonal
while (i < n && j >= 0) {
if (chessboard[i][j]) return false;
i++;
j--;
}
return true;
}
void solveNQueens(int currentColumn) {
if (currentColumn >= n) return;
for (int row = 0; row < n; row++) {
if (isSafe(row, currentColumn)) {
chessboard[row][currentColumn] = 1;
if (currentColumn == n - 1) {
printChessboard();
solutionCount++;
}
solveNQueens(currentColumn + 1);
chessboard[row][currentColumn] = 0; // Backtracking
}
}
}
int main() {
cin >> n;
solveNQueens(0);
cout << "Total solutions: " << solutionCount << endl;
return 0;
}