AcWing 1432. 棋盘挑战
原题链接
中等
作者:
大瓜娃子
,
2021-02-06 10:09:02
,
所有人可见
,
阅读 380
C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 15;
int n;
bool col[N],dg[N*2],udg[N*2];
int path[N],ans;
void dfs(int x)
{
if(x>n)
{
ans++;
if(ans<=3)
{
for(int i=1;i<=n;i++)
{
cout<<path[i]<<" ";
}
cout<<endl;
}
return;
}
for(int y=1;y<=n;y++)
{
if(!col[y]&&!dg[x+y]&&!udg[x-y+n])
{
path[x]=y;
col[y]=dg[x+y]=udg[x-y+n]=true;
dfs(x+1);
col[y]=dg[x+y]=udg[x-y+n]=false;//固定
path[x]=0;
}
}
}
int main()
{
cin>>n;
dfs(1);
cout<<ans<<endl;
return 0;
}