bzoj:1656 [Usaco2006 Jan] The Grove 树木
Description
The pasture contains a small, contiguous grove of trees that has no 'holes' in the middle of the it. Bessie wonders: how far is it to walk around that grove and get back to my starting position? She's just sure there is a way to do it by going from her start location to successive locations by walking horizontally, vertically, or diagonally and counting each move as a single step. Just looking at it, she doesn't think you could pass 'through' the grove on a tricky diagonal. Your job is to calculate the minimum number of steps she must take. Happily, Bessie lives on a simple world where the pasture is represented by a grid with R rows and C columns (1 <= R <= 50, 1 <= C <= 50). Here's a typical example where '.' is pasture (which Bessie may traverse), 'X' is the grove of trees, '*' represents Bessie's start and end position, and '+' marks one shortest path she can walk to circumnavigate the grove (i.e., the answer): ...+... ..+X+.. .+XXX+. ..+XXX+ ..+X..+ ...+++* The path shown is not the only possible shortest path; Bessie might have taken a diagonal step from her start position and achieved a similar length solution. Bessie is happy that she's starting 'outside' the grove instead of in a sort of 'harbor' that could complicate finding the best path.
.jpg)
Input
* Line 1: Two space-separated integers: R and C
* Lines 2..R+1: Line i+1 describes row i with C characters (with no spaces between them).
Output
* Line 1: The single line contains a single integer which is the smallest number of steps required to circumnavigate the grove.
Sample Input
.......
...X...
..XXX..
...XXX.
...X...
......*
Sample Output
随便找棵树然后画一条射线作为分界线,使之不能通过,然后就行BFS啦
以样例为例:
在第二行唯一一棵树那划条射线,然后BFS结果如下:(-1为树木,0为起点)
在这个数据里面并不明显,划过线的部分是不能通过的……
再看这个数据:
结果如下:
其他看代码咯
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std; struct na{
int x,y;
};
int n,m,x=-,y;
const int fx[]={,,,-,,,-,-},fy[]={,-,,,,-,,-};
int map[][];
char c;
queue <na> q;
int main(){
scanf("%d%d",&n,&m);
for (int i=;i<n;i++)
for (int j=;j<m;j++){
c=getchar();
while(c=='\n') c=getchar();
if (c=='X') {
map[i][j]=-;
if (x==-) x=i,y=j;
}else
if (c=='*'){
na tmp;tmp.x=i;tmp.y=j;q.push(tmp);
}else map[i][j]=;
}
while(!q.empty()){
na k=q.front();q.pop();
for (int i=;i<;i++){
na now=k;
now.x+=fx[i];now.y+=fy[i];
if (now.x<||now.y<||now.x>=n||now.y>=m) continue;
if (k.y<=y&&k.x==x&&now.x==x-) continue;
if (k.y<=y&&k.x==x-&&now.x==x) continue;
if (map[now.x][now.y]>map[k.x][k.y]+){
map[now.x][now.y]=map[k.x][k.y]+;
q.push(now);
}
}
}
int ans=;
for (int i=y-;i>=;i--){
if (map[x][i]+map[x-][i]<ans) ans=map[x][i]+map[x-][i];
if (map[x][i]+map[x-][i+]<ans) ans=map[x][i]+map[x-][i+];
if (i) if (map[x][i]+map[x-][i-]<ans) ans=map[x][i]+map[x-][i-];
}
printf("%d\n",ans+);
}
bzoj:1656 [Usaco2006 Jan] The Grove 树木的更多相关文章
- 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 树木(bfs+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1656 神bfs! 我们知道,我们要绕这个联通的树林一圈. 那么,我们想,怎么才能让我们的bfs绕一个 ...
- bzoj1656: [Usaco2006 Jan] The Grove 树木 (bfs+新姿势)
题目大意:一个n*m的图中,“.”可走,“X”不可走,“*”为起点,问从起点开始绕所有X一圈回到起点最少需要走多少步. 一开始看到这题,自己脑洞了下怎么写,应该是可过,然后跑去看了题解,又学会了一 ...
- 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 ...
- bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会【tarjan】
几乎是板子,求有几个size>1的scc 直接tarjan即可 #include<iostream> #include<cstdio> #include<cstri ...
随机推荐
- 【java】多线程同步生产者消费者问题
package 多线程; class Producer implements Runnable{ private Data data; public Producer(Data data){ this ...
- LINUX:alias命令详解
发现目前安装的g++并没有开启选项 -std=c++11,无法使用c++11的新标准及其中的列表初始化.搜索后得到解决方法:键入:alias g++="g++ -std=c++11&quo ...
- cocoapods安装说明,最快安装,以及使用
安装卸载更新新推荐 文章最后 其他问题总结: 1 添加taobao提供的镜像地址:http://ruby.taobao.org/ 移除命令:gem sources --remove https://r ...
- NET Framework 版本和依赖关系
原文:https://docs.microsoft.com/zh-cn/dotnet/framework/migration-guide/versions-and-dependencies 每个版本的 ...
- git push的用法
git push <远程仓库名> <本地分支名>:<远程分支名>
- New Life With 2018
2017年转眼过去了.对自己来说.这一大年是迷茫和认知的一年.我的第一篇博客就这样记录下自己的历程吧 一:选择 从进入这一行到现在已经一年多了,2016年11月份就像所有的应届毕业生一样,都贼反感毕业 ...
- Nginx完整配置配置样例【官方版】
我们主要参考nginx官方给出的完整配置的样例: https://www.nginx.com/resources/wiki/start/topics/examples/full/# 完整摘录如下: n ...
- 不写一行代码,利用常用工具和软件批量下载URL资源
有时候会遇到这种情况:想从某个网站下载一批东西,目标URL是比较规整的,而且结构都一样(仅某些字段不同).但又懒得开IDE专门写个脚本去弄,今天就和大家分享一下,如何利用手边常用的软件和工具,不用写一 ...
- VS2017 调试不能命中断点问题
去掉勾
- 淘宝NPM源的使用
npm作为国外的node仓库安装工具,自然会受到我大长城防火墙的干扰,国内用户在安装相关的资源的时候,会出现安装失败,以及速度很慢的情况.为了解决npm安装的问题,国内出现了很多npm的镜像网址,ta ...