bzoj1656: [Usaco2006 Jan] The Grove 树木 (bfs+新姿势)
题目大意:一个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+新姿势)的更多相关文章
- 【BZOJ】1656:[Usaco2006 Jan]The Grove 树木(bfs+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1656 神bfs! 我们知道,我们要绕这个联通的树林一圈. 那么,我们想,怎么才能让我们的bfs绕一个 ...
- BZOJ 1656 [Usaco2006 Jan] The Grove 树木:bfs【射线法】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1656 题意: 给你一个n*m的地图,'.'表示空地,'X'表示树林,'*'表示起点. 所有 ...
- bzoj:1656 [Usaco2006 Jan] The Grove 树木
Description The pasture contains a small, contiguous grove of trees that has no 'holes' in the middl ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- 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 & ...
- BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )
tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...
- 【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法
[BZOJ1720][Usaco2006 Jan]Corral the Cows 奶牛围栏 Description Farmer John wishes to build a corral for h ...
- [Usaco2006 Jan] Redundant Paths 分离的路径
1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1132 Solv ...
- 【转+自己研究】新姿势之Docker Remote API未授权访问漏洞分析和利用
0x00 概述 最近提交了一些关于 docker remote api 未授权访问导致代码泄露.获取服务器root权限的漏洞,造成的影响都比较严重,比如 新姿势之获取果壳全站代码和多台机器root权限 ...
随机推荐
- Python Road
引子 雁离群兮不知所归,路遥远兮吾将何往 Python Road[第一篇]:Python简介 Python Road[第二篇]:Python基本数据类型 Python Road[第三篇]:Pyth ...
- HTTP基本定义
一.网络的简单定义: 1.http:是www服务器传输超文本向本地浏览器的传输协议.(应用层) 2.IP:是计算机之间相互识别通信的机制.(网络层) 3.TCP:是应用层通信之间通信.(传输层) IP ...
- Unity - Humanoid设置Bip骨骼导入报错
报错如下: 解决: 原因是biped骨骼必须按照Unity humanoid的要求设置,在max中设置如下:
- mysql 按日期统计
按年汇总,统计: select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col ...
- Docker学习笔记总结
Docker学习笔记 https://yeasy.gitbooks.io/docker_practice/content/ 一 环境搭建 Ubuntu安装 .添加软件源的GPG密钥 curl -f ...
- Python高级编程-序列化
在程序运行的过程中,所有的变量都是在内存中,比如,定义一个dict: dict1 = {'name': 'Rob', 'age': 19, 'score': 90} 可以随时修改变量,比如把age改成 ...
- SPOJ 8073 The area of the union of circles(计算几何の圆并)(CIRU)
Description You are given N circles and expected to calculate the area of the union of the circles ! ...
- HDU 1007 Quoit Design(计算几何の最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- 如何遍历一个文件夹(C语言实现)
#include<io.h> #include<stdio.h> int main() { long Handle; struct _finddata_t FileInfo; ...
- 软件工程part5
1.本周psp 2.本周饼状图 3.本周进度条