比赛 NOIP2008集训模拟4 评测结果 AAAAAAAWWTWWW
题目名称 潜入辛迪加 最终得分 53
用户昵称 lc 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-11-13 11:11:39
显示代码纯文本
program syndicate;
 const
   maxn=100000;
   d:array[1..4,1..2] of integer=((1,0),(-1,0),(0,1),(0,-1));
 var
    mark:array[1..50,1..16,0..65536] of boolean;
    n,m,head,tail:longint;
    dep:array[1..maxn] of longint;
    q:array[1..maxn] of record
       x,y,D:longint;
       end;
    a,b:array[1..50,1..16] of longint;

procedure init;
 var
    i,j,k,xx,yy:longint;

 begin
  readln(n,m);
  for i:=1 to n do
   for j:=1 to n do
   read(b[i,j]);
  for i:=1 to n do
   for j:=1 to n do
     if b[i,j]=-2
     then begin
             a[i,j]:=-2;
             for k:=1 to 4 do
                begin
                   xx:=i+d[k,1]; yy:=j+d[k,2];
                   if (xx>=1) and (xx<=n) and (yy>=1) and (yy<=n)
                   then a[xx,yy]:=-2;
                end;
          end;
 for i:=1 to n do
  for j:=1 to n do
  if a[i,j]=0 then a[i,j]:=b[i,j];

 end;


procedure print(ans:longint);

 begin
  writeln(ans);
  close(input); close(output);
  halt;
 end;



procedure main;
 var
     i,x,y,xx,yy,DD,len:longint;

 begin
  head:=1; tail:=1; len:=1;
  q[1].x:=1; q[1].y:=1; q[1].D:=0;
  dep[1]:=0;
  fillchar(mark,sizeof(mark),0);
  mark[1,1,0]:=true;
  repeat
    x:=q[head].x; y:=q[head].y;
    for i:=1 to 4 do
       begin
       xx:=x+d[i,1];  yy:=y+d[i,2];  DD:=q[head].D;
       if (xx>=1) and (xx<=n) and (yy>=1) and (yy<=n)
          then
              if (a[xx,yy]=0) or ((a[xx,yy]>=1) and (a[xx,yy]<=m))
              or ( (a[xx,yy]>m) and ( (1 shl (a[xx,yy]-m)) and DD>0 ) )
              then
                   begin
                   if (a[xx,yy]>=1) and (a[xx,yy]<=m)
                      then DD:=DD or (1 shl a[xx,yy]);
                   if mark[xx,yy,DD] then continue;
                   mark[xx,yy,DD]:=true;
                   tail:=tail mod maxn+1; inc(len);
                   q[tail].x:=xx; q[tail].y:=yy; q[tail].D:=DD;
                   dep[tail]:=dep[head]+1;
                   if (xx=n) and (yy=n) then print(dep[tail]);
                   end;

       end;
      head:=head mod maxn+1; dec(len);
   until len=0;

 end;



begin
  assign(input,'syndicate.in');
  assign(output,'syndicate.out');
  reset(input); rewrite(output);
  init;
  main;
  close(input); close(output);
end.