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.

牧场里有一片树林,林子里没有坑.
    贝茜很想知道,最少需要多少步能围绕树林走一圈,最后回到起点.她能上下左右走,也能走对角线格子.牧场被分成R行C列(1≤R≤50,1≤C≤50).下面是一张样例的地图,其中“.”表示贝茜可以走的空地,  “X”表示树林,  “*”表示起点.而贝茜走的最近的路已经特别地用“+”表示出来.
 
 
 
 
 
题目保证,最短的路径一定可以找到.

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).

    第1行输入R和C,接下来R行C列表示一张地图.地图中的符号如题干所述.

Output

* Line 1: The single line contains a single integer which is the smallest number of steps required to circumnavigate the grove.

    输出最少的步数.

Sample Input

6 7
.......
...X...
..XXX..
...XXX.
...X...
......*

Sample Output

13

随便找棵树然后画一条射线作为分界线,使之不能通过,然后就行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 树木的更多相关文章

  1. BZOJ 1656 [Usaco2006 Jan] The Grove 树木:bfs【射线法】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1656 题意: 给你一个n*m的地图,'.'表示空地,'X'表示树林,'*'表示起点. 所有 ...

  2. 【BZOJ】1656:[Usaco2006 Jan]The Grove 树木(bfs+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1656 神bfs! 我们知道,我们要绕这个联通的树林一圈. 那么,我们想,怎么才能让我们的bfs绕一个 ...

  3. bzoj1656: [Usaco2006 Jan] The Grove 树木 (bfs+新姿势)

      题目大意:一个n*m的图中,“.”可走,“X”不可走,“*”为起点,问从起点开始绕所有X一圈回到起点最少需要走多少步. 一开始看到这题,自己脑洞了下怎么写,应该是可过,然后跑去看了题解,又学会了一 ...

  4. BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )

    tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...

  5. 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 & ...

  6. BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径

    Description 给出一个无向图,求将他构造成双连通图所需加的最少边数. Sol Tarjan求割边+缩点. 求出割边,然后缩点. 将双连通分量缩成一个点,然后重建图,建出来的就是一棵树,因为每 ...

  7. 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 ...

  8. 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 ...

  9. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会【tarjan】

    几乎是板子,求有几个size>1的scc 直接tarjan即可 #include<iostream> #include<cstdio> #include<cstri ...

随机推荐

  1. Winccflexable触摸屏的报警

    1.报警的分类 2.自定义报警分类 3.报警组成 4.Winccflexable中预定义的报警类别 5.报警的确认 6.WinccFlexable报警的显示 1)报警视图 2)报警窗口 3).报警指示 ...

  2. Add to List 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  3. jstree 学习

    最近的项目用到了jstree,因为对官方文档理解不充分,所以很多功能都是在网站上搜索再进行使用的.(我只是大自然的搬运工) 对每一级的节点,右键后出现不同的结果. 在jstree中右键是由 conte ...

  4. @Data 注解引出的 lombok 小辣椒

    今天在看代码的时候, 看到了这个注解, 之前都没有见过, 所以就查了下, 发现还是个不错的注解, 可以让代码更加简洁. 这个注解来自于 lombok,lombok 能够减少大量的模板代码,减少了在使用 ...

  5. python中的virtualenv是干嘛的?

    众所周知,python的各种库跨度比较大,比如如果你开发web的话,一个项目使用的Django是1.8, 而另一个项目使用的Django版本是1.7, 这就给开发人员带来了很大的困扰. 因此,pyth ...

  6. Oracle 启动参数

    查看数据库的SID DB_NAME SERVICE_NAME

  7. 使用 PyCharm 添加 第三方 依赖库

    背景 最近开始搞python, 需要帮助算法同事一起调试程序,在本地安装python以后使用 pip 来安装第三方库. 但是算法同事一直使用的是PyCharm 这个IDE,所以需要与他一起调试的时候也 ...

  8. 安全扫描工具 Netsparker

    Netsparker是一款web应用安全漏洞扫描工具 Netsparter官网:https://www.netsparker.com/web-vulnerability-scanner/,与其他安全扫 ...

  9. 安装MySQL容易出现的问题

    mysql 安装到最后一步时,start service 为失败状态. 解决方法: 方 式1  MySQL安装是出现could not start the service mysql error:0 ...

  10. K:线性表的实现—链表

    单链表的概念:  采用链式存储方式存储的线性表称之为链表,链表中每个节点包含存放数据元素的值的数据域和存放指向逻辑上相邻节点的指针域.若一个节点中只包含一个指针域,则称此链表为单链表. 单链表的特点: ...