program num8;
const n=1500;waymax=4;
dx:array[1..4]of longint=(0,1,0,-1);
dy:array[1..4]of longint=(1,0,-1,0);
type sh=array[1..3,1..3]of longint;
var state:array[1..n]of sh;
father,b:array[1..n]of longint;
yes:sh;
o:array[1..n,1..2]of longint;
top,head,wayn,i,j,k,x,y,xx,yy,q:longint;
can,succ:boolean;
procedure print(v:longint);
var buf:array[1..n]of longint;
m:longint;
begin
m:=0;
while v>0 do
begin
m:=m+1;
buf[m]:=v;
v:=father[v];
end;
for i:=m downto 1 do
begin
for j:=1 to 3 do
write(state[buf[i],j,k]:2);
writeln;
end;
//close(input);close(output);
end;
function check:boolean;
var i,j,k:longint;
begin
check:=true;
for k:=1 to top-1 do
for i:=1 to 3 do
for j:=1 to 3 do
if state[top,i,j]<>state[k,i,j] then exit(false);
end;
begin
//assign(input,'input.txt');
//assign(output,'output.txt');
//reset(input);
//rewrite(output);
for i:=1 to 3 do
begin
for j:=1 to 3 do
begin
read(state[1,i,j]);
readln;
end;
readln;
end;
for i:=1 to 3 do
begin
for j:=1 to 3 do
read(yes[i,j]);
readln;
end;
head:=0;top:=1;father[1]:=0;
repeat
head:=head+1;
for wayn:=1 to waymax do
begin
can:=true;
xx:=o[head,1];yy:=o[head,2];
x:=xx+dx[wayn];y:=yy+dy[wayn];
if((x<1)or(x>3))or((y<1)or(y>3))then can:=false;
if can then
begin
top:=top+1;father[top]:=head;
o[top,1]:=x;o[top,2]:=y;
b[top]:=b[head]+1;
state[top]:=state[head];
state[top,xx,yy]:=state[head,x,y];
state[top,x,y]:=0;
if check then top:=top-1
else
begin
succ:=true;
for i:=1 to 3 do
for j:=1 to 3 do
if state[top,i,j]<>yes[i,j] then succ:=false;
if succ then begin
print(top);halt;end;
end;
end;
end;
until(head>top);
end.
标签:编程