题目大意:一个n*m的图中,“.”可走,“X”不可走,“*”为起点,问从起点开始绕所有X一圈回到起点最少需要走多少步。

一开始看到这题,自己脑洞了下怎么写,应该是可过,然后跑去看了题解,又学会了一个新姿势。。。

上图是样例,红色笔迹是走法,需要走13步。

这显然是bfs题,问题是怎么让bfs绕这坨东西一圈,非常巧妙的思路,从任意一个X节点画一条射线与边界垂直,如下图所示。

当我们从右向左bfs的时候碰到这条线,就停止bfs;当我们绕了一圈从左向右bfs的时候碰到这条线,我们就继续走。

dist[1][x][y]表示从左向右经过这条线之后起点到(x,y)的最短距离,dist[0][x][y]表示没有经过这条线,起点到(x,y)的最短距离。当我们从左向右经过这条线后的bfs我们就只更新dist[1][x][y],最后输出的就是dist[1][起点x][起点y]。

代码如下:

const
dx:array[..]of integer=(,,-,,,,-,-);
dy:array[..]of integer=(,,,-,,-,,-);
var
n,m,i,j,fx,fy,tx,ty:longint;
s:string;
line,map:array[..,..]of boolean;
dist:array[..,..,..]of longint;
v:array[..,..,..]of boolean;
h:array[..,..]of longint; procedure bfs;
var
i,j,k,front,rear,nx,ny,xx,yy,flag,flag2:longint;
begin
for i:= to n do for j:= to m do for k:= to do dist[k,i,j]:=;
dist[,fx,fy]:=;v[,fx,fy]:=true;front:=;rear:=;h[,]:=fx;h[,]:=fy;h[,]:=;
while front<>rear do
begin
inc(front);
nx:=h[front,];ny:=h[front,];flag:=h[front,];
if front= then front:=;
for i:= to do
begin
xx:=nx+dx[i];yy:=ny+dy[i];
if (xx<)or(xx>n)or(yy<)or(yy>m)or(map[xx,yy])or((line[xx,yy] or line[nx,ny])and(yy<=ny))then continue;
if (line[xx,yy])or(flag=) then flag2:= else flag2:=;
if dist[flag,nx,ny]+<dist[flag2,xx,yy] then
begin
dist[flag2,xx,yy]:=dist[flag,nx,ny]+;
if not v[flag2,xx,yy] then
begin
v[flag2,xx,yy]:=true;
if rear= then rear:=;
inc(rear);
h[rear,]:=xx;
h[rear,]:=yy;
h[rear,]:=flag2;
end;
end;
end;
v[flag,nx,ny]:=false;
end;
writeln(dist[,fx,fy]);
end; begin
readln(n,m);
for i:= to n do
begin
readln(s);
for j:= to m do
begin
if s[j]='*' then
begin
fx:=i;fy:=j;
end;
if s[j]='X' then
begin
tx:=i;ty:=j;
map[i,j]:=true;
end;
end;
end;
for i:=tx to n do
line[i,ty]:=true;
bfs;
end.

bzoj1656: [Usaco2006 Jan] The Grove 树木 (bfs+新姿势)的更多相关文章

  1. 【BZOJ】1656:[Usaco2006 Jan]The Grove 树木(bfs+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1656 神bfs! 我们知道,我们要绕这个联通的树林一圈. 那么,我们想,怎么才能让我们的bfs绕一个 ...

  2. BZOJ 1656 [Usaco2006 Jan] The Grove 树木:bfs【射线法】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1656 题意: 给你一个n*m的地图,'.'表示空地,'X'表示树林,'*'表示起点. 所有 ...

  3. bzoj:1656 [Usaco2006 Jan] The Grove 树木

    Description The pasture contains a small, contiguous grove of trees that has no 'holes' in the middl ...

  4. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  5. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan

    1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec  Memory Limit: 64 MB Description The N (2 & ...

  6. BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )

    tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...

  7. 【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法

    [BZOJ1720][Usaco2006 Jan]Corral the Cows 奶牛围栏 Description Farmer John wishes to build a corral for h ...

  8. [Usaco2006 Jan] Redundant Paths 分离的路径

    1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1132  Solv ...

  9. 【转+自己研究】新姿势之Docker Remote API未授权访问漏洞分析和利用

    0x00 概述 最近提交了一些关于 docker remote api 未授权访问导致代码泄露.获取服务器root权限的漏洞,造成的影响都比较严重,比如 新姿势之获取果壳全站代码和多台机器root权限 ...

随机推荐

  1. [JSON].toXMLString()

    语法:[JSON].toXMLString() 返回:[String] 说明:将[JSON]实例转换成XML格式结果. 示例: <% jsonString = "{div: 'hell ...

  2. leetcode-数数并说

     数数并说     报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作  " ...

  3. OpenMPI源码剖析3:try_kill_peers 和 ompi_rte_abort 函数

    接着上一篇的疑问,我们说道,会执行 try_kill_peers 函数,它的函数定义在 ompi_mpi_abort.c 下: // 这里注释也说到了,主要是杀死在同一个communicator的进程 ...

  4. Centos6设置DNS

    通过编辑 vi /etc/resolv.conf 设置首选DNS和次要DNS.如下,排在前面的就是首选DNS,后面一行就是次要的DNS服务器DNS vi /etc/resolv.conf namese ...

  5. HDU 4308 Saving Princess claire_(简单BFS)

    求出不使用P点时起点到终点的最短距离,求出起点到所有P点的最短距离,求出终点到所有P点的最短距离. 答案=min( 不使用P点时起点到终点的最短距离, 起点到P的最短距离+终点到P的最短距离 ) #i ...

  6. vue.js 创建组件 子父通信 父子通信 非父子通信

    1.创建组件 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  7. mysql 复杂查询

    1.同一个表下多次查询: sql语句: select b.* ,(select name from exh_common.medicine_type a where b.p_id = a.id) as ...

  8. lintcode-179-更新二进制位

    179-更新二进制位 给出两个32位的整数N和M,以及两个二进制位的位置i和j.写一个方法来使得N中的第i到j位等于M(M会是N中从第i为开始到第j位的子串) 注意事项 In the function ...

  9. lintcode-178-图是否是树

    178-图是否是树 给出 n 个节点,标号分别从 0 到 n - 1 并且给出一个 无向 边的列表 (给出每条边的两个顶点), 写一个函数去判断这张`无向`图是否是一棵树 注意事项 你可以假设我们不会 ...

  10. lintcode-39-恢复旋转排序数组

    39-恢复旋转排序数组 给定一个旋转排序数组,在原地恢复其排序. 说明 什么是旋转数组? 比如,原始数组为[1,2,3,4], 则其旋转数组可以是[1,2,3,4], [2,3,4,1], [3,4, ...