记录编号 8314 评测结果 AAAAAWAATATTA
题目名称 [BYVoid S3] 潜入辛迪加 最终得分 69
用户昵称 Gravatar苏轼 是否通过 未通过
代码语言 Pascal 运行时间 3.396 s
提交时间 2008-11-13 16:07:49 内存使用 5.08 MiB
显示代码纯文本
program cch(input,output);
const
 xx:array[1..4] of integer=(-1,0,1,0);
 yy:array[1..4] of integer=(0,1,0,-1);
type
 sz=array[0..20] of integer;
 ch=record
     x:integer;
     y:integer;
     k:longint;
     s:sz;
    end;
var
 tmps:sz;
 map,map1:array[1..50,1..50] of integer;
 i,j,n,x1,y1,m,head,tail,k,k1:longint;
 a:array[1..100000] of ch;

function rp(s:sz;k:integer):boolean;
var
 i:integer;
begin
 for i:=1 to s[0] do
  if s[i]=k then exit(true);
 exit(false);
end;

function check(x,y:integer; s:sz):boolean;
var
 i:longint;
 flag:boolean;
begin
 for i:=1 to tail do
  if (a[i].x=x)and(a[i].y=y) then
   begin
    flag:=true;
    if a[i].s[0]=s[0] then
     begin
      for j:=1 to a[i].s[0] do
       if a[i].s[j]<>s[j] then
        begin
         flag:=false;
         break;
        end;
      if flag then exit(false);
     end;
   end;
 exit(true);
end;

procedure ins(x:integer);
var
 i,j:integer;
begin
 j:=tmps[0];
 while (j>0)and(tmps[j]>x) do dec(j);
 for i:=tmps[0] downto j+1 do tmps[i+1]:=tmps[i];
 tmps[j+1]:=x;
 inc(tmps[0]);
end;

begin
 assign(input,'syndicate.in');
 assign(output,'syndicate.out');
 reset(input);
 rewrite(output);
 readln(n,m);
 for i:=1 to n do
  for j:=1 to n do
   read(map1[i,j]);
 for i:=1 to n do
  for j:=1 to n do map[i,j]:=map1[i,j];
 for i:=1 to n do
  for j:=1 to n do
   if map1[i,j]=-2 then
     for k:=1 to 4 do
      begin
       x1:=i+xx[k]; y1:=j+yy[k];
       if (x1>=1)and(x1<=n)and(y1>=1)and(y1<=n) then
        if map[x1,y1]=0 then map[x1,y1]:=-2;
      end;
 head:=1; tail:=1;
 a[1].x:=1; a[1].y:=1; a[1].k:=0; a[1].s[0]:=0;
 repeat
  for i:=1 to 4 do
   begin
    x1:=a[head].x+xx[i]; y1:=a[head].y+yy[i];
    k1:=a[head].k+1; tmps:=a[head].s;
    if (x1>=1)and(x1<=n)and(y1>=1)and(y1<=n) then
     if (map[x1,y1]<>-1)and(map[x1,y1]<>-2) then
      begin
       if (map[x1,y1]>m) and rp(a[head].s,map[x1,y1]) or(map[x1,y1]<=m) then
         if check(x1,y1,tmps) then
          begin
           if (map[x1,y1]<=m)and(map[x1,y1]>0) then ins(map[x1,y1]+m);
           if (x1=n)and(y1=n) then
            begin
             write(k1);
             close(input); close(output);
             halt;
            end;
           inc(tail);
           with a[tail] do
            begin
             x:=x1; y:=y1; s:=tmps;
             k:=k1;
            end;
          end;
      end;
   end;
  inc(head);
 until head>tail;
 close(input);
 close(output);
end.