比赛 NOIP2008集训模拟3 评测结果 AEWTEEEEEA
题目名称 移动骷髅 最终得分 20
用户昵称 maxiem 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-11-12 11:10:45
显示代码纯文本
program klgame;
var
  q:array [0..10000,0..24] of record
    x,y:byte;
    step:integer;
  end;
  sn:byte;
  tmp:char;
  i,j,k,n:integer;
procedure wsearch(t:integer);
var
  tail,head,i,j,k,m:integer;
  flag:boolean;
begin
  tail:=0;head:=1;
  while tail<=head do begin
    for i:=0 to sn do begin
      flag:=false;
      for k:=0 to sn do if (i<>k) and (q[tail,k].x-1<q[tail,i].x) and (q[tail,k].y=q[tail,i].y) then begin
        flag:=true;m:=k;
        break;
      end;
      if flag then begin
        for k:=0 to sn do q[head,k]:=q[tail,k];
        for k:=0 to sn do inc(q[head,k].step);
        q[head,i].x:=q[head,m].x+1;
        inc(head);
      end;
      if (q[head-1,0].x=3) and (q[head-1,0].y=3) then begin
        writeln ('level ',t,':');
        writeln (q[head-1,0].step);
        exit;
      end;

      flag:=false;
      for k:=0 to sn do if (i<>k) and (q[tail,k].x+1>q[tail,i].x) and (q[tail,k].y=q[tail,i].y)then begin
        flag:=true;m:=k;
        break;
      end;
      if flag then begin
        for k:=0 to sn do q[head,k]:=q[tail,k];
        for k:=0 to sn do inc(q[head,k].step);
        q[head,i].x:=q[head,m].x-1;
        inc(head);
      end;
      if (q[head-1,0].x=3) and (q[head-1,0].y=3) then begin
        writeln ('level ',t,':');
        writeln (q[head-1,0].step);
        exit;
      end;

      flag:=false;
      for k:=0 to sn do if (i<>k) and (q[tail,k].y<q[tail,i].y-1) and (q[tail,k].x=q[tail,i].x) then begin
        flag:=true;m:=k;
        break;
      end;
      if (q[head-1,0].x=3) and (q[head-1,0].y=3) then begin
        writeln ('level ',t,':');
        writeln (q[head-1,0].step);
        exit;
      end;
      if flag then begin
        for k:=0 to sn do q[head,k]:=q[tail,k];
        for k:=0 to sn do inc(q[head,k].step);
        q[head,i].y:=q[head,m].y+1;
        inc(head);
      end;
      if (q[head-1,0].x=3) and (q[head-1,0].y=3) then begin
        writeln ('level ',t,':');
        writeln (q[head-1,0].step);
        exit;
      end;

      flag:=false;
      for k:=0 to sn do if (i<>k) and (q[tail,k].y>q[tail,i].y+1) and (q[tail,k].x=q[tail,i].x)then begin
        flag:=true;m:=k;
        break;
      end;
      if flag then begin
        for k:=0 to sn do q[head,k]:=q[tail,k];
        for k:=0 to sn do inc(q[head,k].step);
        q[head,i].y:=q[head,m].y-1;
        inc(head);
      end;
      if (q[head-1,0].x=3) and (q[head-1,0].y=3) then begin
        writeln ('level ',t,':');
        writeln (q[head-1,0].step);
        exit;
      end
    end;
    inc(tail);
  end;
end;
begin
  assign (input,'klgame.in');
  reset (input);
  assign (output,'klgame.out');
  rewrite (output);
  readln (n);
  for i:=1 to n do begin
    fillchar (q,sizeof(q),0);
    sn:=0;
    for j:=1 to 5 do begin
      for k:=1 to 5 do begin
        read (tmp);
        if tmp<>'0' then begin
          inc(sn);
          if tmp='2' then begin
            dec(sn);
            q[0,0].x:=j;
            q[0,0].y:=k;
          end
          else begin
            q[0,sn].x:=j;
            q[0,sn].y:=k;
          end;
        end;
      end;
      readln;
    end;
    wsearch(i);
    readln;
  end;
  close (input);
  close (output);
end.