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
|
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10;
char g[N][N];
int n;
int dx[] = {-1, -1, -1, 1, 1, 1}, dy[] = {-1, 0, 1, -1, 0, 1};
// 检查每行每列是否满足要求
bool check(int x, int y){
for(int i = 0; i < 6; i++){
int x1 = x + dx[i], y1 = y + dy[i];
while(x1 >= 0 && x1 < n && y1 >= 0 && y1 < n){
if(g[x1][y1] == 'Q') return false;
x1 = x1 + dx[i], y1 = y1 + dy[i];
}
}
return true;
}
void dfs(int cur){
if(cur >= n){
for(int i = 0; i < n; i++) cout << g[i] << endl;
cout << endl;
return;
}
for(int i = 0; i < n; i++){
if(check(cur, i)){
g[cur][i] = 'Q';
dfs(cur+1);
g[cur][i] = '.';
}
}
}
int main(){
cin >> n;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
g[i][j] = '.';
}
}
dfs(0);
return 0;
}
|