BZOJ 1656 [Usaco2006 Jan] The Grove 树木:bfs【射线法】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1656
题意:
给你一个n*m的地图,'.'表示空地,'X'表示树林,'*'表示起点。
所有'X'为一个连通块。
对于每一个点,你可以向周围八个方向走,均算作一步。
让你找出一条路径,能够将所有'X'包围。
问你路径最短为多少。
题解:
bfs + 射线法。
找出最上面(x坐标最小)的一个'X',并向上方作一条射线,标记为'#'。
从起点开始bfs,并且不能穿过射线(即'#'不能到达)。
最后枚举射线上的每一个点,令lef为左边能够一步到达当前点的最短路径,rig同理。
所以ans = min (lef + rig + 2)
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#define MAX_N 55
#define INF 1000000000 using namespace std; const int dx[]={-,,,,-,-,,};
const int dy[]={,,-,,-,,-,}; struct Coor
{
int x;
int y;
Coor(int _x,int _y)
{
x=_x;
y=_y;
}
Coor(){}
}; int n,m;
int ans=INF;
int dis[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
char c[MAX_N][MAX_N];
Coor start;
Coor tp;
queue<Coor> q; void read()
{
cin>>n>>m;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
cin>>c[i][j];
if(c[i][j]=='*') start=Coor(i,j);
}
}
} void find_line()
{
tp=Coor(INF,INF);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(c[i][j]=='X' && i<tp.x) tp=Coor(i,j);
}
}
for(int i=tp.x-;i>;i--)
{
c[i][tp.y]='#';
}
} Coor get_front()
{
Coor now=q.front();
q.pop();
vis[now.x][now.y]=false;
return now;
} void insert(Coor now)
{
if(vis[now.x][now.y]) return;
q.push(now);
vis[now.x][now.y]=true;
} inline bool is_legal(int x,int y)
{
return x> && x<=n && y> && y<=m && c[x][y]!='X' && c[x][y]!='#';
} void bfs()
{
memset(dis,0x3f,sizeof(dis));
memset(vis,false,sizeof(vis));
insert(start);
dis[start.x][start.y]=;
while(!q.empty())
{
Coor now=get_front();
int x=now.x;
int y=now.y;
for(int i=;i<;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(is_legal(nx,ny) && dis[nx][ny]>dis[x][y]+)
{
dis[nx][ny]=dis[x][y]+;
insert(Coor(nx,ny));
}
}
}
} void solve()
{
find_line();
bfs();
for(int i=tp.x-;i>;i--)
{
int x=i;
int y=tp.y;
int lef=min(dis[x][y-],min(dis[x-][y-],dis[x+][y-]));
int rig=min(dis[x][y+],min(dis[x-][y+],dis[x+][y+]));
ans=min(ans,lef+rig+);
}
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}
BZOJ 1656 [Usaco2006 Jan] The Grove 树木:bfs【射线法】的更多相关文章
- 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 ...
- bzoj1656: [Usaco2006 Jan] The Grove 树木 (bfs+新姿势)
题目大意:一个n*m的图中,“.”可走,“X”不可走,“*”为起点,问从起点开始绕所有X一圈回到起点最少需要走多少步. 一开始看到这题,自己脑洞了下怎么写,应该是可过,然后跑去看了题解,又学会了一 ...
- 【BZOJ】1656:[Usaco2006 Jan]The Grove 树木(bfs+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1656 神bfs! 我们知道,我们要绕这个联通的树林一圈. 那么,我们想,怎么才能让我们的bfs绕一个 ...
- BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )
tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...
- 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 分离的路径
Description 给出一个无向图,求将他构造成双连通图所需加的最少边数. Sol Tarjan求割边+缩点. 求出割边,然后缩点. 将双连通分量缩成一个点,然后重建图,建出来的就是一棵树,因为每 ...
- bzoj:1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会
Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...
- BZOJ——1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏
http://www.lydsy.com/JudgeOnline/problem.php?id=1720 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1 ...
随机推荐
- html中keydown事件
实现在输入框按回车按钮进行查询的功能: 1.<input type="text" id="inputChannel" onkeydown="ke ...
- C++11并发学习之三:线程同步(转载)
C++11并发学习之三:线程同步 1.<mutex> 头文件介绍 Mutex又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 <mutex> 头文 ...
- 对LCD接口的认识
LCD接口类型: 1.首先我们以传递的信号类型来区分主要有两大类:- 模拟信号: - VGA: Video Graphics Array- 数字信号 - TTL: Transistor Transis ...
- 关于JQ checkbox选择的问题
今天做了一个狠坑爹的事情. $("#dele_chk").bind('click',function(){ if($(this).attr('checked')){ $(" ...
- git入门五(分支合并冲突和衍合)
分支合并冲突的处理 合并分支的冲突时在不同的分支中修改了同一个文件的同一部分,程序无法把两份有差异的文件合并,这时候需要人为的干预解决冲突.当前处于master 分支,当dev 分支和master ...
- sql生成器(含凝视)问题修复版
接上篇http://blog.csdn.net/panliuwen/article/details/47406455 sql生成器--生成含凝视的sql语句 今天我使用自己写的sql生成器了.自我感觉 ...
- liunx 下安装 php_screw 扩展 以及报错处理
php_screw 是一个 php 源代码加密扩展.首先来看一下 php_screw 在liunx下是如何安装的 首先 去源完整下载 安装包,现在的最新版是 1.5,我们就用1.5 来做个实例 如果有 ...
- IOS程序国际化
1.1 新建一个Single View app模版项目,命名为Localization. 1.2 新建后,可以看到工作目录结构文件如下,单击InfoPlist.strings,查看右边的属性,在Loc ...
- Github上好的Android开源框架
1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1) JSON,图像等的异步下载: (2) 网络请求的排序(scheduli ...
- Jmeter 03 Jmeter脚本开发
JMeter 工作区介绍 JMeter Http 协议录制 JMeter 脚本调测 JMeter 关联 JMeter 参数化 JMeter 检查点 JMeter 事务 JMeter 集合点 JMete ...