记录编号 8376 评测结果 AAAAAAAATATTA
题目名称 [BYVoid S3] 潜入辛迪加 最终得分 76
用户昵称 Gravatarbing 是否通过 未通过
代码语言 Pascal 运行时间 4.469 s
提交时间 2008-11-13 20:29:53 内存使用 47.84 MiB
显示代码纯文本
program bing;
type
 biao=array[1..50,1..50] of integer;
 zuobiao=record
 x,y:integer;
end;
var
 f1,f2:text;
 n,m:integer;
 a:biao;
 q1:array[1..10000] of zuobiao;
 q2:array[1..10000] of biao;
procedure init;
var
 i,j:integer;
begin
 assign(f1,'syndicate.in');reset(f1);
 assign(f2,'syndicate.out');rewrite(f2);
 readln(f1,n,m);
 for i:=1 to n do
 for j:=1 to n do
 read(f1,a[i,j]);
 for i:=1 to n do
 for j:=1 to n do
  if a[i,j]=-2 then
   begin
    if (i-1>0)and(a[i-1,j]>=0) then a[i-1,j]:=-1;
    if (j-1>0)and(a[i,j-1]>=0) then a[i,j-1]:=-1;
    if (i+1<=n)and(a[i+1,j]>=0) then a[i+1,j]:=-1;
    if (j+1<=n)and(a[i,j+1]>=0) then a[i,j+1]:=-1;
   end;
end;
procedure xiao(x,y:integer;var at:biao);
var
 i,j:integer;
begin
 for i:=1 to n do
 for j:=1 to n do
 if at[i,j]=at[x,y]+m then
 at[i,j]:=0;
 at[x,y]:=0;
end;
procedure pd(var t:longint);
var
 it,jt,kt:longint;
 f1,f2:boolean;
begin
 for kt:=1 to t-1 do
 begin
  f1:=false;
  if (q1[kt].x<>q1[t].x)or(q1[kt].y<>q1[t].y) then f1:=true;
  if f1=false then
   for it:=1 to n do
   for jt:=1 to n do
   if q2[kt][it,jt]<>q2[t][it,jt] then begin f1:=true;break;end;
  if (f1=false) then begin
                    dec(t);
                    exit;
                   end;
 end;
end;
procedure soso;
const
 go:array[1..4,1..2] of integer=((0,-1),(0,1),(-1,0),(1,0));
var
 i,j,k,h,t,tem:longint;
 at:biao;
 deep:array[1..10000] of longint;
begin
 h:=1;t:=1;
 q1[1].x:=1;q1[1].y:=1;
 deep[1]:=0;
 q2[1]:=a;
 repeat

  for k:=1 to 4 do
  begin
   at:=q2[h];
   i:=q1[h].x;j:=q1[h].y;
   if (i+go[k,1]>0)and(i+go[k,1]<=n)and(j+go[k,2]>0)and(j+go[k,2]<=n) then
   begin
    i:=i+go[k,1];j:=j+go[k,2];
    if at[i,j]<>-1 then
    if (i=n)and(j=n) then begin write(f2,deep[h]+1);close(f1);close(f2);halt;end
    else
    if at[i,j]=0 then begin
                       inc(t);
                       q1[t].x:=i;
                       q1[t].y:=j;
                       q2[t]:=at;
                       deep[t]:=deep[h]+1;
                       pd(t);
                      end
    else
    if (at[i,j]>=1)and(at[i,j]<=m) then begin
                                        inc(t);
                                        q1[t].x:=i;
                                        q1[t].y:=j;
                                        xiao(i,j,at);
                                        q2[t]:=at;
                                        deep[t]:=deep[h]+1;
                                        pd(t);
                                       end;
    end;
  end;
  inc(h);
 until h>t;
end;
begin
 init;
 soso;
 write(f2,-1);
 close(f1);close(f2);
end.