题目描述
考虑将如此安排在一个 3 x3 行列中的九个时钟:
|-------| |-------| |-------| | | | | | | | |---O | |---O | | O | | | | | | | |-------| |-------| |-------| A B C |-------| |-------| |-------| | | | | | | | O | | O | | O | | | | | | | | | | |-------| |-------| |-------| D E F |-------| |-------| |-------| | | | | | | | O | | O---| | O | | | | | | | | | |-------| |-------| |-------| G H I
目标要找一个最小的移动顺序次将所有的指针指向12点。
下面原表格列出了9种不同的旋转指针的方法,每一种方法都叫一次移动。
选择1到9号移动方法,将会使在表格中对应的时钟的指针顺时针旋转90度。
移动方法 | 受影响的时钟 |
1 | ABDE |
2 | ABC |
3 | BCEF |
4 | ADG |
5 | BDEFH |
6 | CFI |
7 | DEGH |
8 | GHI |
9 | EFHI |
Example
9 9 12 9 12 12 9 12 12 12 12 12 12 12 12
6 6 6 5 -> 9 9 9 8-> 9 9 9 4 -> 12 9 9 9-> 12 12 12
6 3 6 6 6 6 9 9 9 12 9 9 12 12 12
[但这可能不是正确的方法,请看下面]
PROGRAM NAME: clocks
INPUT FORMAT
第1-3行: | 三个空格分开的数字,每个数字表示一个时钟的初始时间,3,6,9,12。 数字的含意和上面第一个例子一样。 |
SAMPLE INPUT (file clocks.in)
9 9 12
6 6 6
6 3 6
OUTPUT FORMAT
单独的一行包括一个用空格分开的将所有指针指向12:00的最短移动顺序的列表。
如果有多种方案,输出那种使的连接起来数字最小的方案。(举例来说5 2 4 6 < 9 3 1 1)。
SAMPLE OUTPUT (file clocks.out)
4 5 8 9
var
a,c:array[1..9] of longint;
t:array[1..1000000,1..11] of longint;
b:array[1..1000000,1..11] of longint;
bz:array[1..4,1..4,1..4,1..4,1..4,1..4,1..4,1..4,1..4] of boolean;
x:array[1..9,1..9] of longint=((1,1,0,1,1,0,0,0,0),
(1,1,1,0,0,0,0,0,0),
(0,1,1,0,1,1,0,0,0),
(1,0,0,1,0,0,1,0,0),
(0,1,0,1,1,1,0,1,0),
(0,0,1,0,0,1,0,0,1),
(0,0,0,1,1,0,1,1,0),
(0,0,0,0,0,0,1,1,1),
(0,0,0,0,1,1,0,1,1));
i,j,k,head,tail,tt,ttt:longint;
procedure print(x:longint);
begin
if t[x,10]<>1 then print(t[x,10]);
write(t[x,11],' ');
end;
procedure pd;
var
i,bz:longint;
begin
bz:=0;
for i:=1 to 9 do
if t[tail,i]<>4 then bz:=1;
if bz=0 then
begin
print(tail);
halt;
end;
end;
begin
for i:=1 to 9 do
begin
read(tt);
tt:=tt div 3;
t[1,i]:=tt;
end;
head:=0;
tail:=1;
while head<tail do
begin
inc(head);
for i:=1 to 9 do
a[i]:=t[head,i];
c:=a;
for i:=1 to 9 do
begin
if b[head,i]>2 then continue;
inc(tail);
for j:=1 to 9 do
t[tail,j]:=(a[j]+x[i,j]-1) mod 4+1;
if bz[t[tail,1],t[tail,2],t[tail,3],t[tail,4],t[tail,5],t[tail,6],t[tail,7],t[tail,8],t[tail,9]] then
begin
dec(tail);
continue;
end;
bz[t[tail,1],t[tail,2],t[tail,3],t[tail,4],t[tail,5],t[tail,6],t[tail,7],t[tail,8],t[tail,9]]:=true;
t[tail,10]:=head;
t[tail,11]:=i;
pd;
for j:=1 to 9 do
b[tail,j]:=b[head,j];
inc(b[tail,i]);
end;
end;
end.