//本代码还未ac
#include <bits/stdc++.h>
using namespace std;
int n, m, K;
char mp[15][15];
int a[15], b[15], c[15];// 列 对角线 反对角线
// 列 a[i]
// 对角线 y=-x+b b=y+x b[u+i]
// 反对角线 y=x+b b=y-x+n c[i-u+n]多加了n是为了防止出现负数
void dfs(int u) {
if (u == n) {
for (int i = 1; i <= n; ++i) {
puts(mp[i]);
}
puts("");
return;
}
for (int i = 1; i <= n; ++i) {
if (!a[i] && !b[u + i] && !c[n - u + i]) {
mp[u][i] = 'Q';
a[i] = b[u + i] = c[n - u + i] = 1;
dfs(u + 1);
a[i] = b[u + i] = c[n - u + i] = 1;
mp[u][i] = '.';
}
}
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
mp[i][j] = '.';
}
}
dfs(0);
return 0;
}
来源:CSDN
作者:Zaller
链接:https://blog.csdn.net/Yubing792289314/article/details/104718635