可以看做是有条件的全排列
题解
#include<iostream>
#include<cmath>
using namespace std;
int p[1000000],count =0,n;
bool hashtable[1000000]={false};
void generate(int index){
bool flag = true;
if(index==n+1){
for(int i=1;i<=n;i++){
for(int j =i+1;j<=n;j++){
if(abs(p[i]-p[j])==abs(i-j)){
flag=false;
}
}
}
if(flag){
count++;
return ;
}
}
for(int x= 1;x<=n;x++){
if(hashtable[x]==false){
p[index]=x;
hashtable[x]=true;
generate(index+1);
hashtable[x]=false;
}
}
}
int main(){
cin>>n;
generate(1);
cout<<count<<endl;
return 0;
}