随笔 - 0  文章 - 5  trackbacks - 0
<2025年6月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

常用链接

留言簿(2)

文章分类

文章档案

教育

信息学奥赛

有用网站

在线OJ

专题测试

租房信息

搜索

  •  

最新评论

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<iterator>
 4 using namespace std;
 5 const int N(20);
 6 int p[N];
 7 bool a[N],b[2*N],c[2*N]; 
 8 int n,tot;
 9 void search(int i)
10 {
11     if (i>n)
12     {
13         copy(p+1,p+n+1,ostream_iterator<int>(cout," "));
14         cout<<endl;
15         tot++;
16         return ;
17     }
18     for (int j=1;j<=n;j++)
19         if (!a[j]&&!b[i+j]&&!c[i-j+n])
20         {
21             p[i]=j;
22             a[j]=b[i+j]=c[i-j+n]=true;
23             search(i+1);
24             a[j]=b[i+j]=c[i-j+n]=false;
25         }
26 }
27     
28 int main()
29 {
30     while (cin>>n)
31     {
32         tot=0;
33         memset(a,0,sizeof(a));
34         memset(b,0,sizeof(b));
35         memset(c,0,sizeof(c));
36         search(1);
37         cout<<tot<<endl;
38     }
39     return 0;
40 }
41 

posted on 2011-11-02 12:52 龙在江湖 阅读(195) 评论(0)  编辑 收藏 引用 所属分类: 搜索算法