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];
int dx[]={-,,,};
int dy[]={,,-,};
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();
for(int i = ; i < ; i++)
{
next.x=st.x+dx[i];
next.y=st.y+dy[i];
next.step=st.step+;
if(str[next.x][next.y] !='#' && next.x>= && 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);
for(i = ; i < m ; i++)
{
scanf("%s",str[i]);
if(k) continue;
for(j = ; 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 (bfs 树的直径)

    Labyrinth 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/E Description The northern part ...

  3. poj 1383 Labyrinth

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

  4. POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)

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

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

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

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

    POJ - 3067 题意:有(1-n)个城市自上到下在左边, 另有(1-m)个城市自上到下在右边,共有m条高速公路,现求这m条直线的交点个数,交点不包括在城市处相交. 题解:先将高速公路读入,然后按 ...

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

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

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

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

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

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

随机推荐

  1. VMware Workstation虚拟磁盘文件备份或移植

    一.备份快照 1> 1.点击虚拟机上面的快速备份按钮 2.填写快照名字和备注 快照就生成了. 2>恢复 1.点击恢复按钮,此按钮的功能是直接恢复到上一次备份的节点. 2.或者选后面一个按钮 ...

  2. iOS7改变状态栏文字颜色

    1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的 AppDelegate中在 didFinishLaun ...

  3. css新奇技术及其未来发展

    1.图像替换技术: 图像替换技术是指使用图像替换页面中文本的功能,类似与在页面中插入图像,只是这种方法更为方便,易于代码管理.通常来说,设计者习惯使用有意义的图像去替换一些标题,logo和某些特定的页 ...

  4. apache http server2.2 + tomcat5.5 性能调优

    httpd加tomcat做负载均衡,采用session复制方式共享session,采用http-proxy连接方式,打开status mod 一.没有做httpd和tomcat的启动参数修改,包括jv ...

  5. HashMap的简单实现

    基本概念 Map 别名映射表,也叫关联数组,基本思想是它维护的键-值(对)关联,因此可以用键查找值,也有放入键值的操作,下面根据定义自己来实现一个Map,首先应该想到的是数组,因为大多数Java集合类 ...

  6. android开发学习 ------- 【转】EventBus的学习理解

    EventBus是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间.组件与后台线程间的通信. 比如请求网络,等网络返回时通过Handler或Broadca ...

  7. 3. UITest笔记

    1.    XCUIApplication *app = [[XCUIApplication alloc] init]; App为查询的入口,当界面发生变化,查询数也会随之更新. 即使是先前存储的XC ...

  8. 洛谷P3773 [CTSC2017]吉夫特(Lucas定理,dp)

    题意 满足$b_1 < b_2 < \dots < b_k$且$a_{b_1} \geqslant a_{b_2} \geqslant \dots \geqslant a_{b_k} ...

  9. iOS 网络开发

    http://www.cnblogs.com/kenshincui/p/4042190.html

  10. RecyclerView 缓存机制学习笔记2

    RecyclerView 初始化所有的视图后,调用 去缓存(StaggeredGridLayoutManager), 而不是初始化一次缓存一次 存储后系统又会去调用tryGetViewHolderFo ...