比赛 NOIP2008集训模拟1 评测结果 AEAAAEEEEE
题目名称 血色叛徒 最终得分 40
用户昵称 maxiem 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-11-10 08:42:34
显示代码纯文本
program crusade;
const
  d:array [1..4,1..2] of shortint=((0,-1),(0,1),(-1,0),(1,0));
  max=500;
  maxmn=25000;
type node=record
  time,x,y:integer;
end;
var
  i,new,tx,ty,head,tail,a,b,n,m:integer;
  table:array [1..max,1..max] of integer;
  s:array [1..maxmn] of node;
begin
  fillchar (table,sizeof(table),255);
  fillchar (s,sizeof(s),255);
  assign (input,'crusade.in');
  reset (input);
  assign (output,'crusade.out');
  rewrite (output);
  readln (n,m,a,b);
  new:=0;head:=1;tail:=1;
  for i:=1 to a do begin
    inc(new);
    readln (s[new].x,s[new].y);
  end;
  s[tail].time:=0;
  for i:=head to head+new-1 do begin
    table[s[i].x,s[i].y]:=0;
    s[i].time:=s[tail].time;
  end;
  while tail<=head do begin
    head:=new;new:=head;
    for i:=1 to 4 do begin
      tx:=s[tail].x+d[i,1];ty:=s[tail].y+d[i,2];
      if (tx>=1) and (tx<=n) and (ty>=1) and (ty<=n) then if (table[tx,ty]=-1) then begin
        inc(new);
        s[new].x:=tx;
        s[new].y:=ty;
        s[new].time:=s[tail].time+1;
        table[tx,ty]:=s[new].time;
      end;
    end;
    inc(tail);
  end;
  for i:=1 to b do begin
    readln (tx,ty);
    writeln (table[tx,ty]);
  end;
  close (input);
  close (output);
end.