记录编号 7508 评测结果 AAAAAAAAAA
题目名称 [BYVoid S1] 血色叛徒 最终得分 100
用户昵称 Gravatarrottenwood 是否通过 通过
代码语言 Pascal 运行时间 0.494 s
提交时间 2008-11-10 16:32:41 内存使用 5.84 MiB
显示代码纯文本
program crusade;
const
z:array[1..4,1..2] of integer=((1,0),(0,1),(-1,0),(0,-1));
type
shuzu=array[0..500,0..500] of longint;
shuzu1=array[1..250000,1..2] of longint;
shuzu2=array[1..500,1..500] of longint;
var
s,f:shuzu;
x,y:shuzu1;
a,b,i,j,k,m,n,d,p:longint;
procedure bfs(t,w:longint);
  var
  i,j,k,xx,yy:longint;
  begin
  inc(d);
  for i:=t to w do
   for j:=1 to 4 do
   begin
   xx:=z[j,1]+x[i,1]; yy:=z[j,2]+x[i,2];
   if (xx>0)and(xx<=n)and(yy>0)and(yy<=n)and(f[xx,yy]=-1)
   then begin
   f[xx,yy]:=d;
   inc(p);
   x[p,1]:=xx; x[p,2]:=yy;
   end;
   end;
   if p>=w+1 then bfs(w+1,p);
   end;
begin
assign(input,'crusade.in');reset(input);
assign(output,'crusade.out');rewrite(output);
readln(n,m,a,b);
for i:=1 to n do
 for j:=1 to m do
 f[i,j]:=-1;
for i:=1 to a do
begin
readln(x[i,1],x[i,2]);
f[x[i,1],x[i,2]]:=0;
end;
for i:=1 to b do
readln(y[i,1],y[i,2]);
p:=a; d:=0;
bfs(1,a);
for i:=1 to b do
 writeln(f[y[i,1],y[i,2]]);
  close(output);
  end.