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为起点)

在这个数据里面并不明显,划过线的部分是不能通过的……

再看这个数据:

 

结果如下:

其他看代码咯

  1. #include<queue>
  2. #include<cstdio>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. struct na{
  7. int x,y;
  8. };
  9. int n,m,x=-,y;
  10. const int fx[]={,,,-,,,-,-},fy[]={,-,,,,-,,-};
  11. int map[][];
  12. char c;
  13. queue <na> q;
  14. int main(){
  15. scanf("%d%d",&n,&m);
  16. for (int i=;i<n;i++)
  17. for (int j=;j<m;j++){
  18. c=getchar();
  19. while(c=='\n') c=getchar();
  20. if (c=='X') {
  21. map[i][j]=-;
  22. if (x==-) x=i,y=j;
  23. }else
  24. if (c=='*'){
  25. na tmp;tmp.x=i;tmp.y=j;q.push(tmp);
  26. }else map[i][j]=;
  27. }
  28. while(!q.empty()){
  29. na k=q.front();q.pop();
  30. for (int i=;i<;i++){
  31. na now=k;
  32. now.x+=fx[i];now.y+=fy[i];
  33. if (now.x<||now.y<||now.x>=n||now.y>=m) continue;
  34. if (k.y<=y&&k.x==x&&now.x==x-) continue;
  35. if (k.y<=y&&k.x==x-&&now.x==x) continue;
  36. if (map[now.x][now.y]>map[k.x][k.y]+){
  37. map[now.x][now.y]=map[k.x][k.y]+;
  38. q.push(now);
  39. }
  40. }
  41. }
  42. int ans=;
  43. for (int i=y-;i>=;i--){
  44. if (map[x][i]+map[x-][i]<ans) ans=map[x][i]+map[x-][i];
  45. if (map[x][i]+map[x-][i+]<ans) ans=map[x][i]+map[x-][i+];
  46. if (i) if (map[x][i]+map[x-][i-]<ans) ans=map[x][i]+map[x-][i-];
  47. }
  48. printf("%d\n",ans+);
  49. }

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. SAP RFC介绍:关于sRFC,aRFC,tRFC,qRFC和bgRFC

    大概八月份的时候做过一个有关两个SAP系统的财务集成的项目,使用到了RFC(Remote Function Call)技术.因为之前有着医疗-CRM相关接口开发的经验,以为自己对RFC很熟悉了,做起来 ...

  2. 简单介绍什么是协程及其在ES6中的实现方式

    协程,英文名coroutine,是一种执行过程可以被暂停和恢复的方法.各个协程之间相互协作完成一个任务. 让我们来看一个关于发挥协程作用的例子.假定我们有一个生产者和消费者的关系,生产者创建物品并将物 ...

  3. Spring入门篇总结:

    本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 视频传送门:Spring入门篇 该门课程主要从Spring的Bean ...

  4. HTML篇(上)

    1.Doctype作用?标准模式与兼容模式有什么区别? (1)<!doctype>声明位于HTML文档中的第一行,处于<html>标签之前.告知浏览器的解析器用什么文档标准解析 ...

  5. javascript01

    手敲代码01 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  6. 连接虚拟机mysql无法访问,报错编号1130的解决方法

    新装一台虚拟机mysql的时候,往往会出现win无法连接的情况,报错信息1130,是因为没有权限的问题,解决方案如下: mysql -u root -p mysql>use mysql; mys ...

  7. 阿里云Centos7 apache配置

    其实很简单,主要是有坑. 首先填坑,在阿里云安全策略上开放要访问的端口,然后配置firewall添加对应端口开放. firewall-cmd --zone=public --add-port=8011 ...

  8. 例子:js超级玛丽小游戏

    韩顺平_轻松搞定网页设计(html+css+javascript)_第34讲_js超级玛丽小游戏_学习笔记_源代码图解_PPT文档整理 采用面向对象思想设计超级马里奥游戏人物(示意图) 怎么用通过按键 ...

  9. .Net Core建站(3):搭建三层架构

    啊,终于到写三层架构的时候了,老实说,我都不知道自己这个算不算三层架构,姑且就当它是吧,具体属于哪一个体系,希望有大佬指点一下(^o^)/ 不晓得有人注意到没有,我写了三篇博客,然后就改了三次标题ヽ( ...

  10. PowerShell 函数

    PowerShell 中函数是一系列 PowerShell 语句的组合.当你通过函数的名称调用函数时,函数中的语句会被顺序的执行,就像在命令行中执行它们一样. 从 hello world 开始 fun ...