Description

The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of them either filled by rock, or free. There is also a little hook on the floor in the center of every free block. The ACM have found that two of the hooks must be connected by a rope that runs through the hooks in every block on the path between the connected ones. When the rope is fastened, a secret door opens. The problem is that we do not know which hooks to connect. That means also that the neccessary length of the rope is unknown. Your task is to determine the maximum length of the rope we could need for a given labyrinth.

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers C and R (3 <= C,R <= 1000) indicating the number of columns and rows. Then exactly R lines follow, each containing C characters. These characters specify the labyrinth. Each of them is either a hash mark (#) or a period (.). Hash marks represent rocks, periods are free blocks. It is possible to walk between neighbouring blocks only, where neighbouring blocks are blocks sharing a common side. We cannot walk diagonally and we cannot step out of the labyrinth. 
The labyrinth is designed in such a way that there is exactly one path between any two free blocks. Consequently, if we find the proper hooks to connect, it is easy to find the right path connecting them.

Output

Your program must print exactly one line of output for each test case. The line must contain the sentence "Maximum rope length is X." where Xis the length of the longest path between any two free blocks, measured in blocks.

Sample Input

2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######

Sample Output

Maximum rope length is 0.
Maximum rope length is 8.

Hint

Huge input, scanf is recommended. 
If you use recursion, maybe stack overflow. and now C++/c 's stack size is larger than G++/gcc
 
输入时先判断str[][]是否等于'.',若等于记录'.'的位置bx,by,对bx,by进行搜索,找出离bx,by距离最大的点kx,ky,然后对kx,ky进行搜索找出最大的距离。
从数组中任意一个点开始搜索找到离他最远的点(x,y),则数组中离(x,y)最远的点与(x,y)的距离就是任意两点距离的最大值。
 #include<cstdio>
 #include<queue>
 #include<string.h>
 #define M 1010
 using namespace std;
 int n,m,i,ans,flag[M][M],j,bx,by,kx,ky;
 char str[M][M];
 ]={-,,,};
 ]={,,-,};
 struct stu
 {
     int x,y,step;
 }st;
 void bfs(int xx,int yy)
 {
     memset(flag,,sizeof(flag));
     stu next;
     st.x=xx;
     st.y=yy;
     st.step=;
     queue<stu>que;
     flag[xx][yy] = ;
     que.push(st);
     while(!que.empty())
     {
         st=que.front();
         que.pop();
          ; i < ; i++)
         {
             next.x=st.x+dx[i];
             next.y=st.y+dy[i];
             next.step=st.step+;
              && next.y>= && next.x<m&&next.y<n&& flag[next.x][next.y] == )
             {
                 flag[next.x][next.y]=;
                 if(ans < next.step)
                 {
                     ans=next.step;
                     kx=next.x;
                     ky=next.y;
                 }
                 que.push(next);
             }
         }
     }
 }
 int main()
 {
     int t,k;
     scanf("%d",&t);
     while(t--)
     {
         ans=;

         k=;
         scanf("%d %d",&n,&m);
          ; i < m ; i++)
         {
             scanf("%s",str[i]);
             if(k) continue;
              ; j < n ; j++)
             {
                 if(str[i][j] == '.')
                 {
                     bx=i;
                     by=j;
                     k=;
                 }
             }
         }
         bfs(bx,by);            //搜索出离bx,by距离最远的点kx,ky
         bfs(kx,ky);            //搜索出两点之间最大的距离
         printf("Maximum rope length is %d.\n",ans);
     }
  } 

POJ 1383 Labyrinth (树的直径求两点间最大距离)的更多相关文章

  1. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

  2. poj 1383 Labyrinth

    题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...

  3. POJ 1985 Cow Marathon &amp;&amp; POJ 1849 Two(树的直径)

    树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...

  4. poj 2229 Ultra-QuickSort(树状数组求逆序数)

    题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...

  5. hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. POJ 3067 Japan 树状数组求逆序对

    题目大意:有两排城市,这两排城市之间有一些路相互连接着,求有多少条路相互交叉. 思路:把全部的路先依照x值从小到大排序,x值同样的依照y值从小到大排序,然后插入边的时候,先找有多少比自己y值小的,这些 ...

  7. POJ 1849 Two(树的直径--树形DP)(好题)

    大致题意:在某个点派出两个点去遍历全部的边,花费为边的权值,求最少的花费 思路:这题关键好在这个模型和最长路模型之间的转换.能够转换得到,全部边遍历了两遍的总花费减去最长路的花费就是本题的答案,要思考 ...

  8. BZOJ 3363 POJ 1985 Cow Marathon 树的直径

    题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include < ...

  9. 算法笔记--树的直径 &amp;&amp; 树形dp &amp;&amp; 虚树 &amp;&amp; 树分治 &amp;&amp; 树上差分 &amp;&amp; 树链剖分

    树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...

随机推荐

  1. 转 Eric Raymond对于几大开发语言的评价

    原文见:http://blog.jobbole.com/79421/ [译注]:Eric Raymond是开源运动的领袖人物,对于UNIX开发有很深的造诣,主持开发了fetchmail.他的<大 ...

  2. SSIS -&gt;&gt; Data Flow Design And Tuning

    Requirements: Source and destination system impact Processing time windows and performance Destinati ...

  3. Netty4 自定义Decoder,Encoder进行对象传递

    首先我们必须知道Tcp粘包和拆包的,TCP是个“流”协议,所谓流,就是没有界限的一串数据,TCP底层并不了解上层业务数据的具体含义,它会根据TCP缓冲区的实际数据进行包的划分,一个完整的包可能会被拆分 ...

  4. 基于HTML5 Canvas的饼状图表实现教程

    昨天我们分享了一款基于HTML5的线性图表应用,效果非常不错,可以看在线DEMO或者实现教程.今天我们继续来分享一款基于HTML5的网页图表,它也是利用Canvas绘制的,但是和前面不同的是,这款图表 ...

  5. Makefile与shell脚本区别

    http://blog.chinaunix.net/uid-20672257-id-3345593.html 在Makefile可以调用shell脚本,但是Makefile和shell脚本是不同的.本 ...

  6. javascript根据元素自定义属性获取元素,操作元素

    写在前面:给某个或多个元素自定义属性data-tar,想获取data-tar='123'的元素来进行进一步的操作,如何实现? function getElementByAttr(tag,attr,va ...

  7. 利用python 实现微信公众号群发图片与文本消息功能

    在微信公众号开发中,使用api都要附加access_token内容.因此,首先需要获取access_token.如下: #获取微信access_token def get_token(): paylo ...

  8. FORTH基本堆栈操作

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  9. Google SketchUp Cookbook: (Chapter 2) Following Paths with Follow Me

    软件环境 SketchUp Pro 2018 参考书籍 Google SketchUp Cookbook Follow Me工具 Follow Me工具,将2D图形沿着一条路径挤出生成3D物体. 使用 ...

  10. sort排序在苹果与安卓端不一致问题

    一.问题 在使用sort排序时,若遇到相同数据或非数值数据时,会出现苹果手机与安卓手机排序不一致问题 var arr = [{ "id": "52", &quo ...