记录编号 7661 评测结果 AAAAAAAAAA
题目名称 [BYVoid S1] 血色叛徒 最终得分 100
用户昵称 Gravatarelysian 是否通过 通过
代码语言 Pascal 运行时间 0.549 s
提交时间 2008-11-10 21:51:07 内存使用 5.17 MiB
显示代码纯文本
program elysian;
type
node=record
x,y:longint;
end;
const
fin='crusade.in';fout='crusade.out';
dx:array[1..4] of integer=(1,0,-1,0);
dy:array[1..4] of integer=(0,1,0,-1);
var
c,l:array[1..250000] of node;
f:array[0..510,0..510] of boolean;
t:array[0..510,0..510] of longint;
n,m,a,b:longint;
f1,f2:text;

procedure init;
var
i,t1,t2:longint;
begin
assign(f1,fin);reset(f1);
assign(f2,fout);rewrite(f2);
readln(f1,n,m,a,b);
for i:=1 to a do
begin
readln(f1,c[i].x,c[i].y);
f[c[i].x,c[i].y]:=true;
end;
for i:=1 to b do
begin
readln(f1,l[i].x,l[i].y);
if f[l[i].x,l[i].y]=true then t[l[i].x,l[i].y]:=0
else t[l[i].x,l[i].y]:=-1
end;
close(f1);
end;

procedure print;
var
i:longint;
begin
for i:=1 to b do
with l[i] do
writeln(f2,t[x,y]);
close(f2);
halt;
end;

procedure bfs;
var
i,k,head,tail,time,x1,y1,count,fst,lst:longint;
begin
head:=1;tail:=a;time:=0;count:=0;
repeat
inc(time);
fst:=head;
lst:=tail;
for k:=fst to lst do
 for i:=1 to 4 do
  begin
   x1:=c[k].x+dx[i];y1:=c[k].y+dy[i];
   if (x1<=n)and(x1>0)and(y1<=m)and(y1>0)and(f[x1,y1]=false) then
    begin
     if t[x1,y1]=-1 then
       begin
        t[x1,y1]:=time;
        inc(count);
        f[x1,y1]:=true;
        if count>=b then print;
       end;
     t[x1,y1]:=time;
     inc(tail);
     c[tail].x:=x1;c[tail].y:=y1;
     f[x1,y1]:=true;
    end;
  end;
head:=lst+1;
until head>=tail;
end;

begin
init;
bfs;
print;
end.