题目大意:一个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. 单词 (Play on Words UVA - 10129 )

    题目描述: 原题:https://vjudge.net/problem/UVA-10129 题目思路: 1.明显是判断欧拉路径 2.欧拉路径的两个条件 a.图连通 b.至多为两个奇点,且一个为起点一个 ...

  2. Elasticsearch 评分score计算中的Boost 和 queryNorm

    本来没有这篇文章,在公司分享ES的时候遇到一个问题,使用boost的时候,怎么从评分score中知道boost的影响. 虽然我们从查询结果可以直观看到,boost起了应有的作用,但是在explain的 ...

  3. Leetcode - 461. Hamming Distance n&=(n-1) (C++)

    1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...

  4. HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)

    Description Facer is addicted to a game called "Tidy is learning to swim". But he finds it ...

  5. 阿里云搭建bind服务,外网ip不能用来解析问题解决

    options { listen-on port 53 { any; }; //端口开放any listen-on-v6 port 53 { ::1; }; directory "/var/ ...

  6. redis 编译安装错误问题

    编译redis安装的时候报错如下: make[1]: [persist-settings] Error 2 (ignored) CC adlist.o/bin/sh: cc: command not ...

  7. Thunder团队第二周 - Scrum会议5

    Scrum会议5 小组名称:Thunder 项目名称:爱阅app Scrum Master:苗威 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...

  8. AndroidUI设计之 布局管理器 - 详细解析布局实现

    写完博客的总结 : 以前没有弄清楚的概念清晰化 父容器与本容器属性 : android_layout...属性是本容器的属性, 定义在这个布局管理器的LayoutParams内部类中, 每个布局管理器 ...

  9. 【IdentityServer4文档】- 支持协议

    IdentityServer 实现了以下协议: OpenID Connect OpenID Connect Core 1.0 (spec) OpenID Connect Discovery 1.0 ( ...

  10. TCP系列03—连接管理—2、TCP连接的同时打开和同时关闭

    在前面的内容中我们介绍了TCP连接管理中最常见的三次握手方式和四次挥手的方式.但是有可能A和B两端同时执行主动打开并连接对方或者同时执行主动关闭连接(尽管发生这种情况的可能性比较低低),这个时候的流程 ...