#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define maxn 10 int n; bool vis[maxn]; int sum; int c[maxn]; bool check(int cur,int j){ if(vis[j])return false; for(int i=0;i<cur;i++){ if(j-cur==c[i]-i||j+cur==c[i]+i)return false; } return true; } void dfs(int cur){ if(cur>=n){ sum++; return ; } for(int i=0;i<n;i++){ if(check(cur,i)){ vis[i]=1; c[cur]=i; dfs(cur+1); vis[i]=0; } } } int main() { freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); while(cin>>n){ memset(vis,0,sizeof vis); sum=0; dfs(0); cout<<sum<<endl; } }
原文地址:http://blog.csdn.net/u013497977/article/details/45955213