The Clocks时钟
IOI'94 - Day 2
考虑将如此安排在一个 3X3 行列中的九个时钟:
目标要找一个最小的移动顺序次将所有的指针指向 12 点. 下面原表格列出了 9 种不同的旋转指针的方法,每一种方法都叫一次移动.
选择1到9 号移动方法,将会使在表格中对应的时钟的指针顺时针旋转 90 度.
移动方法 受影响的时钟
1 ABDE
2 ABC
3 BCEF
4 ADG
5 BDEFH
6 CFI
7 DEGH
8 GHI
9 EFHI
[但这可能不是正确的方法,请看下面]
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
===================
枚举方案的确定,减少冗余...
======================
{
ID:jie19952
PROG:clocks
LANG:PASCAL
}
type
re=array[1..3,1..3]of longint;
var
clock:array[1..3,1..3]of longint;
change:array[1..9,1..3,1..3]of longint;
ans_l,ans_t:longint;
ans_st:ansistring;
procedure init;
begin
assign(input,'clocks.in');
assign(output,'clocks.out');
reset(input); rewrite(output);
end;
procedure terminate;
begin
close(input); close(output);
halt;
end;
function pd:boolean;
var
i,j:longint;
begin
pd:=true;
for i:=1 to 3 do
for j:=1 to 3 do
if clock[i,j]<>12 then exit(false);
end;
procedure dfs(t:longint;st:ansistring);
var
i,j:longint;
ls:array[1..3,1..3]of longint;
begin
if t=10 then
begin
//inc(ans_t);
// writeln(ans_l);
//writeln(pd);
// writeln(st);
// writeln;
if pd and (ans_l>length(st)) then
begin
ans_l:=length(st);
ans_st:=st;
end;
exit;
end;
ls:=clock;
dfs(t+1,st);
//clock:=ls;
//if length(st+' '+char(ord('0')+t))>ans_l then exit;
for i:=1 to 3 do
for j:=1 to 3 do
begin
ls[i,j]:=ls[i,j]+change[t,i,j];
if ls[i,j]>12 then
ls[i,j]:=ls[i,j]-12;
end;
clock:=ls;
dfs(t+1,st+char(ord('0')+t)+' ');
//if length(st+char(ord('0')+t)+' '+char(ord('0')+t))>ans_l then exit;
for i:=1 to 3 do
for j:=1 to 3 do
begin
ls[i,j]:=ls[i,j]+change[t,i,j];
if ls[i,j]>12 then
ls[i,j]:=ls[i,j]-12;
end;
clock:=ls;
dfs(t+1,st+char(ord('0')+t)+' '+char(ord('0')+t)+' ');
//if length(st+char(ord('0')+t)+' '+char(ord('0')+t)+' '+char(ord('0')+t))>ans_l then exit;
for i:=1 to 3 do
for j:=1 to 3 do
begin
ls[i,j]:=ls[i,j]+change[t,i,j];
if ls[i,j]>12 then
ls[i,j]:=ls[i,j]-12;
end;
clock:=ls;
dfs(t+1,st+char(ord('0')+t)+' '+char(ord('0')+t)+' '+char(ord('0')+t)+' ');
end;
procedure main;
var
i,j:longint;
begin
for i:=1 to 3 do
for j:=1 to 3 do
read(clock[i,j]);
fillchar(change,sizeof(change),0);
change[1,1,1]:=3; change[1,1,2]:=3;
change[1,2,1]:=3; change[1,2,2]:=3;
change[2,1,1]:=3; change[2,1,2]:=3;
change[2,1,3]:=3;
change[3,1,2]:=3; change[3,1,3]:=3;
change[3,2,2]:=3; change[3,2,3]:=3;
change[4,1,1]:=3; change[4,2,1]:=3;
change[4,3,1]:=3;
change[5,1,2]:=3; change[5,2,1]:=3;
change[5,2,2]:=3; change[5,2,3]:=3;
change[5,3,2]:=3;
change[6,1,3]:=3; change[6,2,3]:=3;
change[6,3,3]:=3;
change[7,2,1]:=3; change[7,2,2]:=3;
change[7,3,1]:=3; change[7,3,2]:=3;
change[8,3,1]:=3; change[8,3,2]:=3;
change[8,3,3]:=3;
change[9,2,2]:=3; change[9,2,3]:=3;
change[9,3,2]:=3; change[9,3,3]:=3;
ans_l:=maxlongint;
ans_t:=0;
dfs(1,'');
writeln(copy(ans_st,1,length(ans_st)-1));
// writeln(ans_t);
end;
begin
init;
main;
terminate;
end.