program clevertyper;
const
max=600000;
type
passtext=array[1..6] of byte; {用数组存储每一个密码数字}
tlist=record {结点类型}
father:longint;
dep:byte;
point:1..6;
state:passtext;
end;
var
source,target:passtext; {初始结点和目标结点}
list:array[0..max] of tlist; {扩展出的中间结点序列}
head,foot,best,i:longint;
answer:longint;
found:boolean;
str1:string[8];
point0:1..6;
procedure init; {初始化过程}
var
i:byte;
begin
assign(input,'clever.in');
reset(input);
assign(output,'clever.out');
rewrite(output);
readln(str1);
for i:=1 to 6 do
val(copy(str1,i,1),source[i]);
val(copy(str1,8,1),point0);
readln(str1);
for i:=1 to 6 do
val(copy(str1,i,1),target[i]);
fillchar(list,sizeof(list),0);
found:=false;
head:=0; {队列初始化,队首指针head,队尾指针foot}
foot:=1;
with list[1] do {初始结点作为队列第一个结点}
begin
state:=source;
dep:=0;
father:=0;
point:=point0;
end;
end;
function same(a,b:passtext):boolean;
var
i:byte;
begin
same:=false;
for i:=1 to 6 do
if a[i]<>b[i] then exit;
same:=true;
end;
function notappear(newv:tlist):boolean; {判断扩展出的结点是否已在队列中的函数}
var
i:longint;
begin
notappear:=false;
for i:=1 to foot do
if same(newv.state,list[i].state) and (newv.point=list[i].point)
then exit;
notappear:=true;
end;
procedure add(newv:tlist); {往队列中加入新结点过程}
begin
if notappear(newv)
then begin
inc(foot);
list[foot]:=newv;
end;
end;
procedure expand(index:longint;var n:tlist); {扩展结点过程}
var
i,x,y:integer;
newv:tlist;
begin
for i:=1 to 6 do {分别应用6条规则}
begin
if i=1 then
if n.point>1
then begin
newv.state:=n.state;
newv.point:=n.point;
newv.state[1]:=n.state[n.point];
newv.state[n.point]:=n.state[1];
end;
if i=2 then
if n.point<6
then begin
newv.state:=n.state;
newv.point:=n.point;
newv.state[6]:=n.state[n.point];
newv.state[n.point]:=n.state[6];
end;
if i=3 then
if n.state[n.point]<9
then begin
newv.state:=n.state;
newv.point:=n.point;
newv.state[n.point]:=newv.state[n.point]+1;
end;
if i=4 then
if n.state[n.point]>0
then begin
newv.state:=n.state;
newv.point:=n.point;
newv.state[n.point]:=newv.state[n.point]-1;
end;
if i=5 then
if n.point>1
then begin
newv.state:=n.state;
newv.point:=n.point-1;
end;
if i=6 then
if n.point<6
then begin
newv.state:=n.state;
newv.point:=n.point+1;
end;
newv.father:=index;
newv.dep:=n.dep+1;
add(newv);
end;
end;
procedure print(index:longint); {递归打印路径}
var
i,j:byte;
begin
if index=0 then exit;
print(list[index].father);
for i:=1 to 6 do
write(list[index].state[i]);
writeln(' ',list[index].point);
end;
begin{main}
init;
repeat
inc(head);
if same(list[head].state,target) {比较是否跟目标相同,相同则找到,否则扩展新结点}
then begin
found:=true;
best:=list[head].dep;
answer:=head;
break;
end;
if list[foot].dep>6 {搜索树的深度超过6时,速度很慢,显示超时}
then begin
writeln('OverTime!');
break;
end;
expand(head,list[head]);
until (head>=foot) or (foot>max) or found;
if found
then begin
writeln(best);
print(answer);
end
else writeln('No Answer');
close(input);
close(output);
end.
//delphi/7189