2015年05月24日

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值