hdoj 2553 N皇后问题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2553

 1 #include<iostream>
 2 #include<cstdlib>
 3 using namespace std;
 4 
 5 int n,tot,x[15];
 6 
 7 void dfs(int t){
 8     if(t>n) tot++;
 9     else{
10         for(int i=1;i<=n;i++){
11             x[t]=i;
12             bool flag=1;
13             for(int j=1;j<t;j++){
14                 if(x[t]==x[j] || abs(x[t]-x[j])==abs(t-j)){
15                     flag=0;
16                     break;
17                 }
18             }
19             if(flag) dfs(t+1);
20         }
21     }
22 }
23 
24 int main(){
25     int a[15];
26     for(n=1;n<=10;n++){
27         tot=0;
28         dfs(1);
29         a[n]=tot;
30     }
31     while(cin>>n,n) cout<<a[n]<<endl;
32     return 0;
33 }

原文地址:https://www.cnblogs.com/linqiuwei/p/3161950.html