AcWing 843. n-皇后问题
原题链接
中等
作者:
piaofan
,
2021-02-20 21:41:54
,
所有人可见
,
阅读 214
#include<iostream>
using namespace std;
const int N = 30;
char g[N][N];
bool col[N], dg[N], udg[N];
int n;
void dfs (int u) {
if (u == n) {
for (int i = 0; i < n; i++) puts(g[i]);
printf("\n");
return;
}
for (int i = 0; i < n; i++) {
if (!col[i] && !dg[n + i - u] && !udg[u + i]) {
g[u][i] = 'Q';
col[i] = dg[n + i - u] = udg[u + i] = true;
dfs(u + 1);
col[i] = dg[n + i - u] = udg[u + i] = false;
g[u][i] = '.';
}
}
}
int main (void) {
cin >> n;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
g[i][j] = '.';
dfs(0);
return 0;
}