You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
829 B

#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int path[N];
int n;
int b1[N], b2[N], b3[N];
void dfs(int u) {
if (u == n + 1) { // 全部行都摆上皇后
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == path[i])
printf("Q");
else
printf(".");
}
puts("");
}
puts("");
return;
}
for (int i = 1; i <= n; i++) { // x行y列
if (!b1[i] && !b2[u + i] && !b3[u - i + 8]) {
path[u] = i;
b1[i] = b2[u + i] = b3[u - i + 8] = 1;
dfs(u + 1);
b1[i] = b2[u + i] = b3[u - i + 8] = 0;
}
}
}
int main() {
cin >> n;
dfs(1);
return 0;
}