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
Sample Output
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为奇数)
然后就是码代码了= =
/**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ var
i,j,k,l,m,n:longint;
begin
readln(n);
case n mod of
,:begin
k:=n div ;
case (k mod )+(n mod )* of
:begin
for i:= to (n-k) div do writeln(k+i*);
for i:= to (k-) div do writeln(+i*);
for i:= to (n-k-) div do writeln(k++i*);
for i:= to k div do writeln(+*i);
end;
:begin
for i:= to (n-k-) div do writeln(k+i*);
for i:= to (k-) div do writeln(+i*);
for i:= to (n-k-) div do writeln(k++i*);
for i:= to k div do writeln(+*i);
writeln(n);
end;
:begin
for i:= to (n-k-) div do writeln(k+i*);
for i:= to (k-) div do writeln(+i*);
for i:= to (n-k-) div do writeln(k++i*);
for i:= to (k-) div do writeln(+*i);
end;
:begin
for i:= to (n-k-) div do writeln(k+i*);
for i:= to (k-) div do writeln(+i*);
for i:= to (n-k-) div do writeln(k++i*);
for i:= to (k-) div do writeln(+*i);
writeln(n);
end;
end;
end;
else begin
if odd(n) then
begin
for i:= to (n-) div do writeln(i*);
for i:= to (n+) div do writeln(i*-);
end
else
begin
for i:= to n div do writeln(i*);
for i:= to n div do writeln(i*-);
end;
end;
end;
readln;
end.