您的位置:首页 > 其它

bzoj 2464 BFS【水】

2016-12-16 21:11 260 查看
水过就好,就当是个好心情=。=

const
walk:array[1..4,1..2] of integer=((1,0),(-1,0),(0,1),(0,-1));
type
rec=record
x,y:longint;
end;

var
n,m,sx,sy,tx,ty :longint;
s :ansistring;
map :array[0..510,0..510] of char;
que :array[0..250010] of rec;
dis :array[0..510,0..510] of longint;
vis :array[0..510,0..510] of boolean;
i,j :longint;
procedure work;
var
i,j:longint;
xx,yy,h,tl,tt:longint;
begin
fillchar(dis,sizeof(dis),127);
dis[sx,sy]:=0;
que[1].x:=sx;
que[1].y:=sy;
h:=0;tl:=1;
//
while (h<>tl) do
begin
h:=h mod 250005+1;
vis[que[h].x,que[h].y]:=false;
//
for i:=1 to 4 do
begin
xx:=que[h].x+walk[i,1];
yy:=que[h].y+walk[i,2];
if (xx>0) and (xx<=n) and (yy>0) and (yy<=m) then
begin
if (map[xx,yy]<>map[que[h].x,que[h].y]) then tt:=1 else tt:=0;
if dis[xx,yy]>dis[que[h].x,que[h].y]+tt then
begin
dis[xx,yy]:=dis[que[h].x,que[h].y]+tt;
if not vis[xx,yy] then
begin
vis[xx,yy]:=true;
tl:=tl mod 250005+1;
que[tl].x:=xx;
que[tl].y:=yy;
end;
end;
end;
end;
end;
end;

begin
readln(n,m);
while (n<>0) and (m<>0) do
begin
fillchar(vis,sizeof(vis),false);
for i:=1 to n do
begin
readln(s);
for j:=1 to m do map[i,j]:=s[j];
end;
//
readln(sx,sy,tx,ty);
inc(sx);inc(sy);inc(tx);inc(ty);
work;
writeln(dis[tx,ty]);
readln(n,m);
end;
end.
——by Eirlys
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bfs bzoj