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.

Example

Sample Input:
2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
####### Sample output:
Maximum rope length is 0.
Maximum rope length is 8.

题意:给定一个N*M的地图,现在要找出地图上面最长的一笔画‘.’的长度。

思路:即是找出最长的连通块的直径。对于每一个块,两次DFS即可。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int dis[maxn][maxn],vis[maxn][maxn];
int cnt,ans,Sx,Sy,Tx,Ty,N,M,times;
int xx[]={,,,-};
int yy[]={,-,,};
char c[maxn][maxn];
void dfs(int x,int y){
vis[x][y]=times;
if(dis[x][y]>ans) ans=dis[x][y];
for(int i=;i<;i++){
if(x+xx[i]>=&&x+xx[i]<=N&&y+yy[i]>=&&y+yy[i]<=M){
if(vis[x+xx[i]][y+yy[i]]!=times&&c[x+xx[i]][y+yy[i]]=='.'){
dis[x+xx[i]][y+yy[i]]=dis[x][y]+;
dfs(x+xx[i],y+yy[i]);
}
}
}
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--){
scanf("%d%d",&M,&N);
memset(dis,,sizeof(dis));
memset(vis,,sizeof(vis));
Sx=Sy=Tx=Ty=ans=;
for(i=;i<=N;i++) scanf("%s",c[i]+);
for(i=;i<=N;i++)
for(j=;j<=M;j++)
if(c[i][j]=='.'&&!vis[i][j]){
Sx=Tx=i; Sy=Ty=j; times++;
dis[Sx][Sy]=; dfs(Sx,Sy); //第一次dfs
for(i=;i<=N;i++)
for(j=;j<=M;j++)
if(dis[i][j]>dis[Tx][Ty]) Tx=i,Ty=j;
dis[Tx][Ty]=; times++;
dfs(Tx,Ty); //第二次dfs
}
printf("Maximum rope length is %d.\n",ans);
}
return ;
}

SPOJ:Labyrinth(最大直线)的更多相关文章

  1. UVALive 4639 && SPOJ SPOINTS && POJ 3805 && AOJ 1298 Separate Points 求两个凸包是否相交 难度:3

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  2. MFC中如何画带实心箭头的直线

    工作中遇到话流程图的项目,需要画带箭头的直线,经过摸索,解决:思路如下: (1) 两个点(p1,p2)确定一个直线,以直线的一个端点(假设p2)为原点,设定一个角度 (2)以P2为原点得到向量P2P1 ...

  3. 水平可见直线 bzoj 1007

    水平可见直线 (1s 128M) lines [问题描述] 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆 ...

  4. SVG:linearGradient渐变在直线上失效的问题解决方案

    SVG开发里有个较为少见的问题. 对x1=x2或者y1=y2的直线(line以及path),比如: <path d="M200,10 200,100" stroke=&quo ...

  5. 封装 用canvas绘制直线的函数--面向对象

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  7. [bzoj1007][HNOI2008][水平可见直线] (斜率不等式)

    Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为 可见的,否则Li为被覆盖的. 例如,对于直线: L1:y ...

  8. [LeetCode] Line Reflection 直线对称

    Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...

  9. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

随机推荐

  1. zoj 2835 Magic Square(set)

    Magic Square Time Limit: 2 Seconds      Memory Limit: 65536 KB In recreational mathematics, a magic ...

  2. 二分图最大权完美匹配KM算法

    KM算法二分图 KM求得二分图与普通二分图的不同之处在于:此二分图的每条边(男生女生)上都附了权值(好感度).然后,求怎样完美匹配使得权值之和最大. 这,不止一般的麻烦啊. 可以通过一个期望值来求. ...

  3. SQL ROW_NUMBER() 通用分页存储过程

    --提取分页数据,返回总记录数 Create procedure [dbo].[sp_Common_GetDataPaging_ReturnDataCount] ( @SqlString varcha ...

  4. HDU 3911 区间合并求最大长度的问题

    http://vjudge.net/problem/viewProblem.action?id=21557 题目大意: 每进行一次颜色改变都可以把一段区间内的黑石头变成白石头,白石头变成黑石头,最后问 ...

  5. [luoguP2513] [HAOI2009]逆序对数列(DP)

    传送门 f[i][j]表示前i个数,逆序对数为j的答案 则DP方程为: f[1][0] = 1; for(i = 2; i <= n; i++) for(j = 0; j <= m; j+ ...

  6. [luoguP2015] 二叉苹果树(DP)

    传送门 貌似是个树形背包... 好像吧.. f[i][j]表示节点i选条边的最优解 #include <cstdio> #include <cstring> #include ...

  7. 【数学】codeforces C. Maximal GCD

    http://codeforces.com/contest/803/problem/C [题意] 给定两个数n,k(1 ≤ n, k ≤ 10^10) 要你输出k个数,满足以下条件: ①这k个数之和等 ...

  8. mappedBy的具体使用及其含义

    mappedBy: 1>只有OneToOne,OneToMany,ManyToMany上才有mappedBy属性,ManyToOne不存在该属性: 2>mappedBy标签一定是定义在被拥 ...

  9. [Usaco2006 Nov] Fence Repair 切割木板

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1356  Solved: 714[Submit][Status][Discuss] Description ...

  10. Linux kernel 内核学习路线

    看了下各位大神的推荐路线,总结如下: 0. 跟着项目走: 1. 学会用.熟练用linux系统: 2. Linux Kernel Development. 3. Understanding the Li ...