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. python-001 第一个Python3.x程序 hello world

    我们可以使用以下命令来查看我们使用的Python版本: (d:\ProgramData\Anaconda3) C:\Users\Administrator.2016-20160920ET>pyt ...

  2. Dream City(线性DP)

    描述 JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the ya ...

  3. bzoj1059:[ZJOI2007]矩阵游戏【二分图匹配】

    Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两 ...

  4. [Bzoj1297][Scoi2009 ]迷路 (矩阵乘法 + 拆点)

    1297: [SCOI2009]迷路 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1385  Solved: 993[Submit][Status] ...

  5. mysql too many connection 解决办法

    SHOW VARIABLES LIKE "max_connections"; SHOW VARIABLES LIKE "wait_timeout"; SET G ...

  6. 无线网卡与本地连接不能同时使用&一机多网络的优先级设置

    无线网卡与本地连接不能同时使用&一机多网络的优先级设置 2012-05-30 20:39 初次记录 2012-08-09 10:32 修订 题目中的两个问题,其实都可以归结为一个问题,即网络优 ...

  7. Activiti-5.3工作流引擎-源码解析(流程文档解析)

    前面我们通过BPMN20.xsd和Activiti自定义的XML Schema文件初步了解了业务流程模型的定义,那么现在我们来了解一下流程文档的解析过程,这个过程主要是通过代码解析来完成. 代码解析过 ...

  8. Meteor集合

    在本教程中,我们将学习如何使用 MongoDB集合. 创建集合 我们可以使用以下代码来创建一个新的集合- meteorApp/client/main.js MyCollection = new Mon ...

  9. Linux性能诊断工具

    vmstat:虚拟内存状况 –swpd   free  buff  cache   si  so   in   cs 參考:http://www.cnblogs.com/ggjucheng/archi ...

  10. 分享codeigniter框架,在zend studio 环境下的代码提示

    一.到github下载相关文件 https://github.com/Stunt/Codeigniter-autocomplete 二.把文件放到application/config中 代码提示就出来 ...