AcWing 843. n-皇后问题
原题链接
中等
作者:
梦都会空
,
2021-12-06 18:03:41
,
所有人可见
,
阅读 129
#include <iostream>
using namespace std;
const int N = 1010;
int n;
char a[N][N];
bool h[N],zx[N],fx[N];
void dfs(int x)
{
if(x==n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j];
}
cout<<endl;
}
cout<<endl;
return ;
}
for(int i=0;i<n;i++)
{
if(!h[i]&&!zx[i+x]&&!fx[n-x+i])
{
a[x][i]='Q';
h[i]=zx[i+x]=fx[n-x+i]=true;
dfs(x+1);
h[i]=zx[i+x]=fx[n-x+i]=false;
a[x][i]='.';
}
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]='.';
}
}
dfs(0);
return 0;
}