-
Notifications
You must be signed in to change notification settings - Fork 0
/
N皇后问题_1.cpp
66 lines (54 loc) · 1.78 KB
/
N皇后问题_1.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
#include <iostream>
#include <vector>
#include <string>
#define int long long
using namespace std;
int n, cnt = 0;
vector<vector<string>> solutions;
vector<string> board;
int cols = 0; // 使用整数表示列的占用状态
int diagonals = 0; // 使用整数表示主对角线的占用状态
int anti_diagonals = 0; // 使用整数表示副对角线的占用状态
bool is_safe(int row, int col) {
return !((cols >> col) & 1) && !((diagonals >> (row - col + (n - 1))) & 1) && !((anti_diagonals >> (row + col)) & 1);
}
void place_queen(int row, int col) {
board[row][col] = 'Q';
cols |= (1 << col); // 设置列的占用状态
diagonals |= (1 << (row - col + (n - 1))); // 设置主对角线的占用状态
anti_diagonals |= (1 << (row + col)); // 设置副对角线的占用状态
}
void remove_queen(int row, int col) {
board[row][col] = '.';
cols &= ~(1 << col); // 清除列的占用状态
diagonals &= ~(1 << (row - col + (n - 1))); // 清除主对角线的占用状态
anti_diagonals &= ~(1 << (row + col)); // 清除副对角线的占用状态
}
void backtrack(int row) {
if(row == n) {
solutions.push_back(board);
++cnt;
return;
}
for(int col = 0; col < n; col++) {
if(is_safe(row, col)) {
place_queen(row, col);
backtrack(row + 1);
remove_queen(row, col);
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin >> n;
board.resize(n, string(n, '.'));
backtrack(0);
// for (const auto& solution : solutions) {
// for (const auto& row : solution) {
// cout << row << endl;
// }
// cout << endl; // 每个解之间添加空行以分隔
// }
cout << "共有 " << cnt << " 种解决方案" << endl;
return 0;
}