比赛 NOIP2008集训模拟1 评测结果 WTWWWTTTTT
题目名称 血色叛徒 最终得分 0
用户昵称 thegy 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-11-10 11:19:56
显示代码纯文本
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..250000]of longint;
  xx,yy,cc:longint;
  i,j,tot,tail,head:longint;
function isgoal(xxx,yyy:longint):longint;
var
  ii:longint;
begin
  isgoal:=-1;
  for ii:=1 to tot do
    if (goal[ii].x=xxx) and (goal[ii].y=yyy) then begin
      isgoal:=ii;
      break;
    end;
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 a do begin
    read(fin,xx,yy);
    inc(tail);
    queue[tail].x:=xx;
    queue[tail].y:=yy;
    queue[tail].c:=0;
  end;
  head:=1;
  j:=0;
  for i:=1 to b do begin
    read(fin,xx,yy);
    inc(j);
    goal[j].x:=xx;
    goal[j].y:=yy;
  end;
  tot:=j;
  repeat
    xx:=queue[head].x;
    yy:=queue[head].y;
    cc:=queue[head].c;
    j:=isgoal(xx,yy);
    if j<>-1 then ans[j]:=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<=n 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 tot do writeln(fout,ans[i]);
  close(fin);
  close(fout);
end.