记录编号 7465 评测结果 AAAAAAAAAA
题目名称 [BYVoid S1] 血色叛徒 最终得分 100
用户昵称 Gravatarthegy 是否通过 通过
代码语言 Pascal 运行时间 0.537 s
提交时间 2008-11-10 14:36:24 内存使用 7.03 MiB
显示代码纯文本
program crusade;
type
  node=record
         x,y:longint;
         c:longint;
       end;
var
  fin,fout:text;
  n,m,a,b:longint;
  queue:array[1..250000]of node;
  goal:array[1..250000]of node;
  hash:array[1..500,1..500]of boolean;
  ans:array[1..500,1..500]of longint;
  xx,yy,cc:longint;
  i,j,tot,tail,head:longint;
function isgoal(x,y:longint):boolean;
begin
  if ans[x,y]=-2 then isgoal:=true
  else isgoal:=false;
end;
begin
  assign(fin,'crusade.in'); reset(fin);
  assign(fout,'crusade.out'); rewrite(fout);
  read(fin,n,m,a,b);
  head:=0;
  tail:=0;
  for i:=1 to n do
    for j:=1 to m do hash[i,j]:=false;
  for i:=1 to n do
    for j:=1 to m do ans[i,j]:=-1;
  for i:=1 to a do begin
    read(fin,xx,yy);
    inc(tail);
    queue[tail].x:=xx;
    queue[tail].y:=yy;
    queue[tail].c:=0;
    hash[xx,yy]:=true;
  end;
  for i:=1 to b do begin
    read(fin,xx,yy);
    goal[i].x:=xx;
    goal[i].y:=yy;
    ans[xx,yy]:=-2;
  end;
  head:=1;
  repeat
    xx:=queue[head].x;
    yy:=queue[head].y;
    cc:=queue[head].c;
    if isgoal(xx,yy) then ans[xx,yy]:=cc;
    if xx-1>0 then begin
      if not(hash[xx-1,yy]) then begin
        inc(tail);
        queue[tail].x:=xx-1;
        queue[tail].y:=yy;
        queue[tail].c:=cc+1;
        hash[xx-1,yy]:=true;
      end;
    end;
    if xx+1<=n then begin
      if not(hash[xx+1,yy]) then begin
        inc(tail);
        queue[tail].x:=xx+1;
        queue[tail].y:=yy;
        queue[tail].c:=cc+1;
        hash[xx+1,yy]:=true;
      end;
    end;
    if yy-1>0 then begin
      if not(hash[xx,yy-1]) then begin
        inc(tail);
        queue[tail].x:=xx;
        queue[tail].y:=yy-1;
        queue[tail].c:=cc+1;
        hash[xx,yy-1]:=true;
      end;
    end;
    if yy+1<=m then begin
      if not(hash[xx,yy+1]) then begin
        inc(tail);
        queue[tail].x:=xx;
        queue[tail].y:=yy+1;
        queue[tail].c:=cc+1;
        hash[xx,yy+1]:=true;
      end;
    end;
    inc(head);
  until head>tail;
  for i:=1 to b do writeln(fout,ans[goal[i].x,goal[i].y]);
  close(fin);
  close(fout);
end.