AcWing 843. n-皇后问题
原题链接
简单
作者:
qgc123
,
2021-11-29 08:23:26
,
所有人可见
,
阅读 179
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
int n, ans[15][15], xy[30], yx[30], l[30];
void dfs(int u){
if(u == n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(ans[i][j])cout << "Q";
else cout << ".";
}
cout << endl;
}
cout <<endl;
return;
}
for(int i = 0; i < n; i++){
if(!xy[u+i] && !yx[n-u+i]&&!l[i]){
xy[u+i] = 1;
yx[n-u+i] = 1;
ans[u][i] = 1;
l[i] = 1;
dfs(u+1);
l[i] = 0;
xy[u+i] = 0;
yx[n-u+i] = 0;
ans[u][i] = 0;
}
}
}
signed main(){
cin >> n;
dfs(0);
return 0;
}