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. @setupmethod -- flask.setupmethod

    源码: def setupmethod(f): """Wraps a method so that it performs a check in debug mode i ...

  2. Android CircleImageView圆形ImageView

     Android CircleImageView圆形ImageView CircleImageView是github上一个第三方开源的实现圆形ImageView的项目.其在github上的项目主页 ...

  3. NIUDAY 11.23 北京站抢票啦 | 看 AI 落地行业 享 AI 时代红利

    2018 年是见证「奇迹」的一年.AI 从多年的热门话题中开始走下神坛,逐渐深入到了各个行业,加速经济结构优化及行业智慧化升级,AI 已不再是难以企及的神话而是可触摸的美好未来. 政策支持加上资本推动 ...

  4. 斗地主(codevs 4610)

    题目描述 Description 牛牛最近迷上了一种叫斗地主的扑克游戏. 斗地主是一种使用黑桃.红心.梅花.方片的 A 到 K 加上大小王的共 54 张牌来进行的扑克牌游戏.在斗地主中, 牌的大小关系 ...

  5. 汕头市赛srm8 C-3

    n<=100000个点m<=300000条边有权无向联通图,给出K<=10000个特殊点求K个点中任意两点最短路的最小值. 方法一:K小,随便搞.先构造最短路树,在最短路树上Dijk ...

  6. msp430项目编程07

    msp430中项目---简单计算器 1.扫描键盘工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习

  7. python学习之-- mysql模块和sqlalchemy模块

    简单介绍python下操作mysql数据库模块有2个:pyhton-mysqldb  和 pymysql 说明:在python3中支持mysql 的模块已经使用pymysql替代了MysqlDB(这个 ...

  8. 从头开始学Android之(一)——— Android架构

    从事Android开发已经两年多了,最近项目上特别清闲,刚开始时在闲暇的时候都不知道干嘛,整天混日子.有一天突然有个以前同学找到我,说要我帮忙做一个Android的需求,就是在后台截屏(涉及到服务以及 ...

  9. windows下开发PHP扩展dll(无需Cygwin)

    windows下开发php扩展网上很多资料都说需要Cygwin,其实完全可以不必安装该东东.没错,是可以在linux下生成骨架后拷到windos下来用,但是,如果没有linux环境呢?什么,装虚拟机? ...

  10. HTML的简单学习

    <html>与</html>之间的部分用来描述网页. <body>与</body>之间是页面的可见的内容. <h1>与</h1> ...