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权限 ...
随机推荐
- WEB中间件漏洞--IIS
1.iis安装 版本 一直下一步,选上iis安装 端口修改 网站目录 通过网站发布目录(发布目录任意),新建index.html页面,可以正常访问 2.iis6.0解析漏洞 (1)文件名解析 IIS在 ...
- 用最简单的MVC模式输出内容
MVC是模型(model)-视图(view)-控制器(controller)的缩写,它的作用是使代码分离,可维护性高.重用性高 编写Model层: <?php class model{ publ ...
- python selenium 使用htmlunit 执行测试。非图形界面浏览器。
其实就是换个浏览器,只是这个浏览器没有图形界面而已. browser = webdriver.Chrome() 换成 browser = webdriver.Remote(desired_capabi ...
- Map Reduce Application(Partitioninig/Binning)
Map Reduce Application(Partitioninig/Group data by a defined key) Assuming we want to group data by ...
- vmware安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题
虚拟机使用的是VMware Workstation,并且首次在虚拟机体验64 位系统.在新建好虚拟机,运行时候就出现了VMware Workstation 的提醒:此主机支持 Intel VT-x,但 ...
- vue学习笔记之:为何data是一个方法
vue学习笔记之:为何data是一个方法 在vue开发中,我们可以发现,data中的属性值是在function中return出来的.可为何data必须是一个函数呢?我们先看官方的解释: 当一个组件被定 ...
- C++ 学习笔记之 引用
一.定义: 引用就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样. 二.用法: 基本用法 例如: int & a = b; 引用作为函数返回值 先看一个例子: #inclu ...
- 模拟Excel同一列相同值的单元格合并
背景 项目中有一个查询工作量,可以将查询的结果导出到Excel表中.在Excel工具中,有一个合并居中功能,可以将选中的单元格合并成一个大的单元格.现在需要在程序中直接实现查询结果的汇总, 问题分析 ...
- <Effective C++>读书摘要--Templates and Generic Programming<一>
1.The initial motivation for C++ templates was straightforward: to make it possible to create type-s ...
- 【Docker 命令】- run命令
docker run :创建一个新的容器并运行一个命令 语法: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明: -a stdin: 指 ...