|
发表于 2023-3-22 20:20:03
|
显示全部楼层
#include<iostream>
using namespace std;
int n=4,fangfa=0,a[14],num;
bool c[14],d[2*14],e[2*14];
void dfs(int x){
if(x==n){
fangfa++;
if(fangfa<=3){
for(int i=1;i<=n;i++) cout<<a[i]<<" ";
cout<<'\n';
}
return;
}
for(int i=1;i<=n;i++){
if(!c[i]&&!d[n+x-i]&&!e[x+i]){
a[x]=i;
c[i]=d[n+x-i]=e[x+i]=1;
dfs(x+1);
c[i]=d[n+x-i]=e[x+i]=0;
}
}
return;
}
int main(){
cin>>num;
n=num;
dfs(1);
cout<<fangfa;
return 0;
} |
|