比赛 NOIP2008集训模拟1 评测结果 AEEEEEEEEE
题目名称 血色叛徒 最终得分 10
用户昵称 卐祭司卐 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-11-10 11:22:22
显示代码纯文本
program crusade;
const
 f:array[1..4,1..2] of integer=
  ((-1,0),(0,-1),(0,1),(1,0));
var
 s:array[1..500,1..500] of integer;
 m,n,start,a,b,i,j,t,e,p,q:longint;
 ma,k,temp:array[1..500,1..2] of integer;
 fi,fo:text;

begin
 assign(fi,'crusade.in');
 assign(fo,'crusade.out');
 reset(fi); rewrite(fo);
 readln(fi,n,m,a,b);
 for i:=1 to 500 do for j:=1 to 500 do s[i,j]:=9999;
 for i:=1 to a do begin
   readln(fi,k[i,1],k[i,2]);
   s[k[i,1],k[i,2]]:=0;
   end;
 for i:=1 to b do readln(fi,ma[i,1],ma[i,2]);
 t:=a; start:=1;
 j:=0;
 repeat
  t:=t+p;
  j:=j+1;  p:=0; q:=0;
  for i:=start to t do
   for e:=1 to 4 do if (f[e,1]+k[i,1]>0)and(f[e,1]+k[i,1]<=n)
                    and(f[e,2]+k[i,2]>0)and(f[e,2]+k[i,2]<=m)
                    then begin
                    if (j<s[f[e,1]+k[i,1],f[e,2]+k[i,2]])
                       and(s[f[e,1]+k[i,1],f[e,2]+k[i,2]]=9999) then  begin
                        s[f[e,1]+k[i,1],f[e,2]+k[i,2]]:=j;
                        p:=p+1; k[t+p,1]:=f[e,1]+k[i,1]; k[t+p,2]:=f[e,2]+k[i,2];end
               else if (j<s[f[e,1]+k[i,1],f[e,2]+k[i,2]])
                       and(s[f[e,1]+k[i,1],f[e,2]+k[i,2]]<>9999) then begin
                        s[f[e,1]+k[i,1],f[e,2]+k[i,2]]:=j;
                        q:=q+1;temp[q,1]:=f[e,1]+k[i,1]; temp[q,2]:=f[e,2]+k[i,2];
                     end;

   end;
   for i:=1 to q do begin k[t-i,1]:=temp[i,1];k[t-i,2]:=temp[i,2]; end;
   start:=t+1-q;
 until t=m*n;
 for i:=1 to b do
 writeln(fo,s[ma[i,1],ma[i,2]]);
 close(fi);close(fo);
end.