比赛 NOIP2008集训模拟1 评测结果 AAAAAAAAAA
题目名称 血色叛徒 最终得分 100
用户昵称 Achilles 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-11-10 09:13:57
显示代码纯文本
program crusade;
var
  table:array[1..500,1..500]of integer;
  table2:array[1..500,1..500]of 0..1;
  asz,bsz,d:array[1..250000]of record
    x,y:integer;
  end;
  n,m,a,b,i,p1,p2:longint;
procedure floodfill;
begin
  while p1<p2 do
  begin
    p1:=p1+1;
    if d[p1].x-1>0 then begin
      if table2[d[p1].x-1,d[p1].y]=0 then begin
        table[d[p1].x-1,d[p1].y]:=table[d[p1].x,d[p1].y]+1;
        table2[d[p1].x-1,d[p1].y]:=1;
        p2:=p2+1;
        d[p2].x:=d[p1].x-1;
        d[p2].y:=d[p1].y;
      end;
    end;
    if d[p1].x+1<=n then begin
      if table2[d[p1].x+1,d[p1].y]=0 then begin
        table[d[p1].x+1,d[p1].y]:=table[d[p1].x,d[p1].y]+1;
        table2[d[p1].x+1,d[p1].y]:=1;
        p2:=p2+1;
        d[p2].x:=d[p1].x+1;
        d[p2].y:=d[p1].y;
      end;
    end;
    if d[p1].y-1>0 then begin
      if table2[d[p1].x,d[p1].y-1]=0 then begin
        table[d[p1].x,d[p1].y-1]:=table[d[p1].x,d[p1].y]+1;
        table2[d[p1].x,d[p1].y-1]:=1;
        p2:=p2+1;
        d[p2].x:=d[p1].x;
        d[p2].y:=d[p1].y-1;
      end;
    end;
    if d[p1].y+1<=m then begin
      if table2[d[p1].x,d[p1].y+1]=0 then begin
        table[d[p1].x,d[p1].y+1]:=table[d[p1].x,d[p1].y]+1;
        table2[d[p1].x,d[p1].y+1]:=1;
        p2:=p2+1;
        d[p2].x:=d[p1].x;
        d[p2].y:=d[p1].y+1;
      end;
    end;
  end;
end;
begin
  fillchar(table,sizeof(table),0);
  fillchar(table2,sizeof(table2),0);
  assign(input,'crusade.in');
  assign(output,'crusade.out');
  reset(input);
  rewrite(output);
  readln(n,m,a,b);
  for i:=1 to a do
  begin
    readln(asz[i].x,asz[i].y);
    table2[asz[i].x,asz[i].y]:=1;
  end;
  for i:=1 to b do
    readln(bsz[i].x,bsz[i].y);
  p1:=0;
  p2:=a;
  for i:=1 to a do
    d[i]:=asz[i];
  floodfill;
  for i:=1 to b do
    writeln(table[bsz[i].x,bsz[i].y]);
  close(input);
  close(output);
end.