记录编号 140832 评测结果 AAAAAAAAAA
题目名称 [福州培训2010] 01迷宫 最终得分 100
用户昵称 Gravatar思邈然 是否通过 通过
代码语言 Pascal 运行时间 0.400 s
提交时间 2014-11-25 21:15:42 内存使用 8.78 MiB
显示代码纯文本
program cogs975;
var
map:array[0..1001,0..1001]of char;
b:array[0..1001,0..1001]of longint;
ans:array[0..1001,0..1001]of longint;
ch:char;
n,m,i,j,x,y,t:longint;
procedure bfs;
var
ax:array[1..1000000]of longint;
ay:array[1..1000000]of longint;
tail,head,i,j,k,r,l,ii,o,p:longint;
x:array[1..4]of integer;
y:array[1..4]of integer;
begin
x[1]:=0;x[2]:=1;x[3]:=-1;x[4]:=0;
y[1]:=1;y[2]:=0;y[3]:=0;y[4]:=-1;
for i:=1 to n do
  for j:=1 to n do
  if b[i,j]=0 then begin
  inc(t);
   b[i,j]:=t;
   tail:=1; head:=1;
   ax[head]:=i; ay[head]:=j;
   repeat
   for k:=1 to 4 do begin
     r:=ax[head]+x[k];l:=ay[head]+y[k];
     if (b[r,l]=0)and(r>0)and(r<=n)and(l>0)and(l<=n)and(map[ax[head],ay[head]]<>map[r,l]) then begin
     b[r,l]:=t; inc(tail);
     ax[tail]:=r;ay[tail]:=l; end;  end;
   inc(head);
   until head>tail;
   for ii:=1 to tail do
      ans[ax[ii],ay[ii]]:=tail;
   end;
end;
begin
assign(input,'maze01.in');reset(input);
assign(output,'maze01.out');rewrite(output);
readln(n,m);
for i:=1 to n do begin
 for j:=1 to n do
 read(map[i,j]);
 readln;  end;
fillchar(b,sizeof(b),0);
bfs;
for i:=1 to m do begin
readln(x,y);
writeln(ans[x,y]);end;
close(input);close(output);
end.