3101: N皇后

3101: N皇后

Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special Judge
Submit: 88  Solved: 41
[Submit][Status][Discuss]

Description

n*n的棋盘,在上面摆下n个皇后,使其两两间不能相互攻击…

 

Input

一个数n

 

Output

第i行表示在第i行第几列放置皇后

 

Sample Input

4

Sample Output

2
4
1
3



HINT

 

100%的数据3<n<1000000。输出任意一种合法解即可

 

Source

 

题解:一道神(dou)奇(bi)的题目,传说中貌似有种O(N)构造N皇后解的方法,具体为啥貌似也查不到,求神犇给出证明orzorzorz(引自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为奇数)

(上面序列第i个数为ai,表示在第i行ai列放一个皇后;... 省略的序列中,相邻两数以2递增。下同)

二、当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为奇数)

然后就是码代码了= = 
 
 1 /**************************************************************
 2     Problem: 3101
 3     User: HansBug
 4     Language: Pascal
 5     Result: Accepted
 6     Time:1832 ms
 7     Memory:224 kb
 8 ****************************************************************/
 9  
10 var
11    i,j,k,l,m,n:longint;
12 begin
13      readln(n);
14      case n mod 6 of
15           2,3:begin
16                    k:=n div 2;
17                    case (k mod 2)+(n mod 2)*2 of
18                         0:begin
19                                for i:=0 to (n-k) div 2 do writeln(k+i*2);
20                                for i:=0 to (k-4) div 2 do writeln(2+i*2);
21                                for i:=0 to (n-k-4) div 2 do writeln(k+3+i*2);
22                                for i:=0 to k div 2 do writeln(1+2*i);
23                         end;
24                         2:begin
25                                for i:=0 to (n-k-1) div 2 do writeln(k+i*2);
26                                for i:=0 to (k-4) div 2 do writeln(2+i*2);
27                                for i:=0 to (n-k-5) div 2 do writeln(k+3+i*2);
28                                for i:=0 to k div 2 do writeln(1+2*i);
29                                writeln(n);
30                         end;
31                         1:begin
32                                for i:=0 to (n-k-1) div 2 do writeln(k+i*2);
33                                for i:=0 to (k-3) div 2 do writeln(1+i*2);
34                                for i:=0 to (n-k-3) div 2 do writeln(k+3+i*2);
35                                for i:=0 to (k-1) div 2 do writeln(2+2*i);
36                         end;
37                         3:begin
38                                for i:=0 to (n-k-2) div 2 do writeln(k+i*2);
39                                for i:=0 to (k-3) div 2 do writeln(1+i*2);
40                                for i:=0 to (n-k-4) div 2 do writeln(k+3+i*2);
41                                for i:=0 to (k-1) div 2 do writeln(2+2*i);
42                                writeln(n);
43                         end;
44                    end;
45           end;
46           else begin
47                if odd(n) then
48                   begin
49                        for i:=1 to (n-1) div 2 do writeln(i*2);
50                        for i:=1 to (n+1) div 2 do writeln(i*2-1);
51                   end
52                else
53                    begin
54                         for i:=1 to n div 2 do writeln(i*2);
55                         for i:=1 to n div 2 do writeln(i*2-1);
56                    end;
57           end;
58      end;
59      readln;
60 end.

 

 
posted @ 2015-05-06 23:17  HansBug  阅读(233)  评论(0编辑  收藏  举报