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 ...
随机推荐
- qt调用simsimi api实现小黄鸡
项目地址:https://github.com/racaljk/xiaojianji 好吧我把它命名为小贱鸡.,下面说一说他的实现. 由于官方的simsimi api需要收费,免费试用版有各种限制,所 ...
- Java多线程之赛跑游戏
在JavaSE中,多线程是一个重要的内容. 我们要了解多线程的概念,就要先了解进程的概念:要了解进程的概念,就离不开操作系统的概念. 在一台正常运行的电脑中,计算机硬件(如CPU.内存.硬盘.网卡.显 ...
- 通过 JS 实现简单的拖拽功能并且可以在特定元素上禁止拖拽
前言 关于讲解 JS 的拖拽功能的文章数不胜数,我确实没有必要大费周章再写一篇重复的文章来吸引眼球.本文的重点是讲解如何在某些特定的元素上禁止拖拽.这是我在编写插件时遇到的问题,其实很多插件的拖拽功能 ...
- myecplise自带的tomcat问题
今天做一个项目时候,发现myecplise自带的tomcat上面部署了是可以运行的,可是当部署到自己下载的tomcat时候,就报错,tomcat可以启动,项目无法启动,查了问题,发现是web,xml中 ...
- 安装supervisord
一:简介 supervisord是一个进程管理工具,提供web页面管理,能对进程进行自动重启等操作. 优点: - 可以将非后台运行程序后台运行 - 自动监控,重启进程 缺点: - 不能管理后台运行程序 ...
- 通过js添加的元素点击事件无法触发
var blk_have ='<div class="sw-off"></div>'; $('#blk').prepend(blk_have); $(doc ...
- Mysql的硬件优化和配置优化
mysql数据库的优化,算是一个老生常谈的问题了,网上也有很多关于各方面性能优化的例子,今天我们要谈的是MySQL硬件优化和系统参数的优化-即优化my.cnf文件 MySQL的优化我分为两个部分,一是 ...
- 如何更改 iOS 和安卓浏览器上的 input[type="radio"] 元素的默认样式?
Safari 上的默认样式是这样的, 背景颜色可以使用background-color改变,但中间那个点始终无法去掉. 我查了一些jQuery插件,如iCheck.js,但是那说明写得我都看不明白,根 ...
- vim置于后台,vim 编辑多文件
这里介绍一个很实用的方法:1.将vim置于后台,直接按 ctrl + z可以将当前的vim置于后台 2.然后可以去别的目录再打开一个 当你需要打开之前的vim的时候3.打jobs命令看当前有哪些vim ...
- ES6(三)数组的扩展
1.Array.form ES6中,Array.from = function(items,mapfn,thisArg) { } Array.from 用于将 类数组 和 可遍历对象(实现了It ...