bzoj 1656 bfs+射线法

来源:互联网 发布:网络教育发展趋势 编辑:程序博客网 时间:2024/06/05 21:10

题意: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