poj 3239(n皇后问题)

(网上找的。。。)构造法公式(序列):
一、当n mod 6 != 2 && n mod 6 != 3时:


[2,4,6,8,...,n],[1,3,5,7,...,n-1]        (n为偶数)


[2,4,6,8,...,n-1],[1,3,5,7,...,n ]       (n为奇数)

二、当n mod 6 == 2  n mod 6 == 3


(n为偶数,k=n/2;当n为奇数,k=(n-1)/2)


[k,k+2,k+4,...,n],[2,4,...,k-2],[k+3,k+5,...,n-1],[1,3,5,...,k+1]         (k为偶数,n为偶数)

[k,k+2,k+4,...,n-1],[2,4,...,k-2],[k+3,k+5,...,n-2],[1,3,5,...,k+1],[n]     (k
为偶数,n为奇数)

[k,k+2,k+4,...,n-1],[1,3,5,...,k-2],[k+3,...,n],[2,4,...,k+1]              (k为奇数,n为偶数)

[k,k+2,k+4,...,n-2],[1,3,5,...,k-2],[k+3,...,n-1],[2,4,...,k+1],[n ]      (k
为奇数,n为奇数)

 

(上面有六条序列。一行一个序列,中括号是我额外加上的,方便大家辨认子序列,子序列与子序列之间是连续关系,无视中括号就可以了。第i个数为ai,表示在第iai列放一个皇后;... 省略的序列中,相邻两数以2递增。)

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <cmath>
 6 
 7 using namespace std;
 8 
 9 
10 void queens_puzzle(int n)//n>=8
11 {
12     if(n%6!=2 && n%6!=3)
13     {
14         printf("2");
15         for(int i=4;i<=n;i+=2)
16             printf(" %d",i);
17         for(int i=1;i<=n;i+=2)
18             printf(" %d",i);
19         printf("\n");
20     }
21     else
22     {
23         int k=n/2;
24         if(n%2==0 && k%2==0)
25         {
26             printf("%d",k);
27             for(int i=k+2;i<=n;i+=2)
28                 printf(" %d",i);
29             for(int i=2;i<=k-2;i+=2)
30                 printf(" %d",i);
31             for(int i=k+3;i<=n-1;i+=2)
32                 printf(" %d",i);
33             for(int i=1;i<=k+1;i+=2)
34                 printf(" %d",i);
35         }
36         else if(n%2==1 && k%2==0)
37         {
38             printf("%d",k);
39             for(int i=k+2;i<=n-1;i+=2)
40                 printf(" %d",i);
41             for(int i=2;i<=k-2;i+=2)
42                 printf(" %d",i);
43             for(int i=k+3;i<=n-2;i+=2)
44                 printf(" %d",i);
45             for(int i=1;i<=k+1;i+=2)
46                 printf(" %d",i);
47             printf(" %d",n);
48         }
49         else if(n%2==0 && k%2==1)
50         {
51             printf("%d",k);
52             for(int i=k+2;i<=n-1;i+=2)
53                 printf(" %d",i);
54             for(int i=1;i<=k-2;i+=2)
55                 printf(" %d",i);
56             for(int i=k+3;i<=n;i+=2)
57                 printf(" %d",i);
58             for(int i=2;i<=k+1;i+=2)
59                 printf(" %d",i);
60         }
61         else
62         {
63             printf("%d",k);
64             for(int i=k+2;i<=n-2;i+=2)
65                 printf(" %d",i);
66             for(int i=1;i<=k-2;i+=2)
67                 printf(" %d",i);
68             for(int i=k+3;i<=n-1;i+=2)
69                 printf(" %d",i);
70             for(int i=2;i<=k+1;i+=2)
71                 printf(" %d",i);
72             printf(" %d",n);
73         }
74         printf("\n");
75     }
76 }
77 
78 int main()
79 {
80     int n;
81     freopen("input.txt","r",stdin);
82     freopen("out1.txt","w",stdout);
83     while(~scanf("%d",&n))
84     {
85         if(!n) break;
86         queens_puzzle(n);
87     }
88     return 0;
89 }

 

 

posted @ 2012-10-19 00:18  Missa  阅读(1270)  评论(0编辑  收藏  举报