[题目来源]:USACO 4.4.1
[关键字]:数学
[题目大意]:一个1*2n+1的棋盘,左边有n个白子,中间一个空格,右边n个黑子,求出将其交换的步骤。
//==========================================================================================================
[分析]:http://www.nocow.cn/index.php/USACO/shuttle
[代码]:
View Code
1 {
2 ID:x yr;
3 PROB:shuttle
4 LANG:PASCAL
5 }
6 var
7 n, s: longint;
8 a: array[0..200] of longint;
9
10 procedure init;
11 var
12 i, c: longint;
13 begin
14 readln(n);
15 s := 4;
16 i := 1;
17 c := 5;
18 while i < n do
19 begin
20 s := s+c;
21 inc(i);
22 inc(c,2);
23 end;
24 //writeln(s);
25 //readln;
26 end;
27
28 procedure work;
29 var
30 i, l, c1, c2: longint;
31 begin
32 i := 1;
33 l := 1;
34 c1 := -1;
35 c2 := 2;
36 while i <= s div 2 do
37 begin
38 a[i] := c1;
39 for i := i+1 to i+l do
40 a[i] := c2;
41 inc(l);
42 c1 := -c1;
43 c2 := -c2;
44 inc(i);
45 end;
46 c1 := 0;
47 l := s div 2;
48 if not odd(n) then begin inc(l); inc(c1); end;
49 for i := l to s do
50 begin
51 a[i] := a[i-c1];
52 inc(c1,2);
53 end;
54 //for i := 1 to s do write(a[i],'');
55 //readln;
56 l := n+1;
57 for i := 1 to s-2 do
58 begin
59 l := l+a[i];
60 if i mod 20 = 0 then writeln(l) else write(l,'');
61 end;
62 l := l+a[s-1];
63 writeln(l);
64 //readln;
65 end;
66
67 begin
68 assign(input,'shuttle.in');reset(input);
69 assign(output,'shuttle.out');rewrite(output);
70 init;
71 work;
72 close(input);
73 close(output);
74 end.