# bzoj 1656 bfs+射线法

type
rec=record
x,y:longint;
end;

const
walk:array[1..8,1..2] of longint=((1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,-1),(-1,1));

var
n,m,sx,sy,tx,ty,ans      :longint;
i,j                      :longint;
s                        :string;
map                      :array[0..55,0..55] of char;
dis                      :array[0..55,0..55] of longint;
que                      :array[0..2510] of rec;
procedure bfs;
var
h,tl,curx,cury,k,xx,yy:longint;
begin
h:=0; tl:=1; que[1].x:=sx;
que[1].y:=sy; dis[sx,sy]:=0;
while (h<>tl) do
begin
h:=h mod 2505+1;
curx:=que[h].x;  cury:=que[h].y;
for k:=1 to 8 do
begin
xx:=curx+walk[k,1]; yy:=cury+walk[k,2];
if (xx>0) and (yy>0) and (xx<=n) and (yy<=m) then
if map[xx,yy]='.' then
begin
if ((curx=tx) and (cury>ty) and (xx>tx)) then continue;
if (yy>ty) and (curx>tx) and (xx=tx) then continue;
dis[xx,yy]:=dis[curx,cury]+1;
tl:=tl mod 2505+1;
que[tl].x:=xx;
que[tl].y:=yy;
map[xx,yy]:='X';
end;
end;
end;
end;

begin
for i:=1 to n do
begin
for j:=1 to m do
begin
map[i,j]:=s[j];
if map[i,j]='X' then
begin
tx:=i; ty:=j;
end else
if map[i,j]='*' then
begin
sx:=i; sy:=j; map[i,j]:='X';
end;
end;
end;
bfs;
ans:=maxlongint;
for i:=ty+1 to m do
begin
if dis[tx,i]+dis[tx+1,i]<ans then ans:=dis[tx,i]+dis[tx+1,i];
if i<m then
if dis[tx,i]+dis[tx+1,i+1]<ans then ans:=dis[tx,i]+dis[tx+1,i+1];
if i>0 then
if dis[tx,i]+dis[tx+1,i-1]<ans then ans:=dis[tx,i]+dis[tx+1,i-1];
end;
writeln(ans+1);
end.

——by Eirlys

02-17 2881
05-15 844
03-17 809
04-05 299
12-10 28
07-13 739
04-05 393
10-04 295
10-18 83
06-15 1万+