#include<iostream>
#include<cstdio>
#include<cmath>
#include<bitset>
using namespace std;
#define lowbit(x) (x&(-x)) //取x的最低位
int n,mask;
int ans[15],cnt;
inline int loc(int x)
{
int res=0;
while(x>1) {x/=2;res++;}
return res;
}
void func(int a,int b,int c,int r)
{
if(r==n+1)
{
cnt++;
if(cnt<=3)
{
for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
cout<<"\n";
}
return;
}
int d=(~(a | b<<1 | c>>1))&mask;
while(d)
{
int p=lowbit(d);
if(p&mask==0) return; //没有符合条件的位置
d-=p;
ans[r]=loc(p)+1;
func(a|p,(b<<1)|p,(c>>1)|p,r+1);
}
}
int main()
{
cin>>n;
mask=pow(2,n)-1; //设置掩码,将超出n位的部分去掉
//cout<<bitset<32>(mask);
func(0,0,0,1);
cout<<cnt<<"\n";
return 0;
}
解释写在了注释里
1427

被折叠的 条评论
为什么被折叠?



