标签:
Description
Input
Output
Sample Input
Sample Output
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 const int maxn=11; 5 int b[maxn],a[maxn],sum,n; 6 7 void dfs(int cur) 8 { 9 if(cur == n+1)//递归边界,就有一种摆法 10 sum++; 11 else 12 for(int j = 1; j <=n; j++) 13 { 14 int ok=1; 15 a[cur] = j;//尝试把第cur行的皇后放在第j列 16 for(int i = 1; i<cur; i++) //检查是否和前面的皇后冲突 17 if(a[i] == a[cur] || abs(i - cur) == abs(a[i] - a[cur])) 18 { 19 ok=0; 20 break; 21 } 22 if(ok) 23 dfs(cur+1);//如果合法,继续递归 24 } 25 } 26 27 int main() 28 { 29 for(int i = 1; i <=maxn; i++) 30 { 31 sum = 0; 32 n= i; 33 dfs(1); 34 b[i] = sum; 35 } 36 while(cin>>n && n) 37 cout<<b[n]<<endl; 38 return 0; 39 }
1 #include <cstdio> 2 main() 3 { 4 int n,a[13]={0,1,0,0,2,10,4,40,92,352,724,2680,14200}; 5 while(scanf("%d",&n)) 6 printf("%d\n",a[n]); 7 }
标签:
原文地址:http://www.cnblogs.com/hfc-xx/p/4668054.html