-
Notifications
You must be signed in to change notification settings - Fork 0
/
NQueen.cpp
65 lines (59 loc) · 1.77 KB
/
NQueen.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
#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
void printboard(vector<string> &board){
system("cls"); // Clear the console
for(int i = 0; i < board.size(); i++){
for(int j = 0; j < board[i].size(); j++){
cout << board[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
Sleep(200);
}
void backtrack(int r, int n, unordered_set<int>& col, unordered_set<int>& posdiag, unordered_set<int>& negdiag, vector<string>& board, vector<vector<string>>& res) {
if (r == n) {
res.push_back(board);
return;
}
for (int c = 0; c < n; ++c) {
if (col.count(c) || posdiag.count(r + c) || negdiag.count(r - c)) {
continue;
}
col.insert(c);
posdiag.insert(r + c);
negdiag.insert(r - c);
board[r][c] = 'Q';
printboard(board); // Print the current board state
backtrack(r + 1, n, col, posdiag, negdiag, board, res);
col.erase(c);
posdiag.erase(r + c);
negdiag.erase(r - c);
board[r][c] = '.';
printboard(board); // Print the board state after backtracking
}
}
vector<vector<string>> solveNQueens(int n) {
unordered_set<int> col;
unordered_set<int> posdiag; // (r + c)
unordered_set<int> negdiag; // (r - c)
vector<vector<string>> res;
vector<string> board(n, string(n, '.'));
printboard(board);
backtrack(0, n, col, posdiag, negdiag, board, res);
return res;
}
int main() {
int n;
cout << "Enter the number of queens: ";
cin >> n;
vector<vector<string>> ans = solveNQueens(n);
for(int i = 0; i < ans.size(); i++){
for(int j = 0; j < ans[i].size(); j++){
cout << ans[i][j] << "\n";
}
cout << "\n";
}
return 0;
}