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. Centos 6.x 搭建 Zabbix Server

      zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让 ...

  2. django_models表设计

    和很多现代的web框架一样,django依赖于强大的数据访问层,试图将python面向对象特性和关系型数据库联系起来. 可移植性:不同的数据库,可以使用同一段代码,不用关心后台是哪家的数据库. 在一个 ...

  3. Random Query CodeForces - 846F

    题目 翻译: 给出一个n个数字的数列a[1],...,a[n],f(l,r)表示使a[l],a[l+1],...,a[r]组成的新序列中的重复元素只保留一个后,剩下元素的数量(如果l>r,则在计 ...

  4. AO-XXXX

    一 AO4419:应用于开关应用或PWM应用的场效应管.

  5. WPF日常需要使用的操作

    窗体如何居中弹出 在窗体上添加属性  WindowStartupLocation="CenterScreen" 窗体如何隐藏掉windows边框 添加属性WindowStyle=& ...

  6. unity内存管理

    最近一直在研究unity的内存加载,因为它是游戏运行的重中之重,如果不深入理解和合理运用,很可能导致项目因内存太大而崩溃. 详细说一下细节概念:AssetBundle运行时加载:来自文件就用Creat ...

  7. ES6学习笔记(12)----Reflect

    参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ Reflect 1.概述:Object对象的内部方法都能在Reflect中找到,同时Reflec ...

  8. 严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:解决

    严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal Contain ...

  9. bt设置指定的ip地址

    auto eth0 iface eth0 inet staticaddress 192.168.1.112 IP地址netmask 255.255.255.0 子网掩码network 192.168. ...

  10. IOS代码收集

    http://mobile.51cto.com/hot-410417.htm 退回输入键盘: - (BOOL) textFieldShouldReturn:(id)textField{ [textFi ...