AcWing 1432. 棋盘挑战(二进制解法)
原题链接
中等
作者:
dongdong
,
2021-01-29 11:39:48
,
所有人可见
,
阅读 319
#include<bits/stdc++.h>
using namespace std;
int n,t,ans[25],ji;
int find(int x)
{
int cnt = 0;
while(x)
{
x >>= 1;
cnt ++;
}
return cnt;
}
void dfs(int x,int a,int b,int c)
{
if(a == t)
{
ji ++;
if(ji <= 3)
{
for(int i = 1;i <= n;i ++)
cout<<ans[i]<<" ";
puts("");
}
return;
}
int pos = ~(a|b|c) & t;
while(pos)
{
int p = pos & -pos;
ans[x] = find(p);
dfs(x + 1,a | p,(b | p) >> 1,(c | p) << 1);
pos -= p;
}
}
int main()
{
cin>>n;
t = (1 << n) - 1;
dfs(1,0,0,0);
cout<<ji;
}