bzoj 1656 bfs+射线法

61 篇文章 0 订阅
4 篇文章 0 订阅

题意:n*m的地图,给定起点,每一步可以向八个方向前进,求环绕障碍点一周最少的步数

显然是bfs,但是并不是裸的bfs

射线法见:http://www.mamicode.com/info-detail-1138212.html

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
   readln(n,m);
   for i:=1 to n do
   begin
      readln(s);
      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



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值