bzoj 2464 BFS【水】

4 篇文章 0 订阅

水过就好,就当是个好心情=。=

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值