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
题目大意:在给定的图中寻找两个“.”之间最大的距离
思路  :两次DFS即可第一遍随便找个点,寻找到这个点最远的点p,第二遍DFS以p点开始,寻找到P点最远的点
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
char arr[][];
int start_i,start_j;
int mark[][];
//int arr1[1500][1500];
int ans=;
int xx,yy;
int d[][]={{,},{,},{,-},{-,}};
void dfs(int x,int y,int step){
if(step>ans){
ans=step;//寻找距离x,y最远的点并记录下来
xx=x;
yy=y;
} for(int i=;i<;i++)
{
int dx=x+d[i][];
int dy=y+d[i][];
if(dx>=&&dy>=&&dx<n&&dy<m&&arr[dx][dy]=='.'&&mark[dx][dy]==){
mark[dx][dy]=;
dfs(dx,dy,step+);
mark[dx][dy]=;//回溯
}
}
}
int main()
{
int t;
cin>>t;
while(t--){ cin>>m>>n;//n行m列
for(int i=;i<n;i++){
scanf("%s",&arr[i]);
}
int j;
for(int i=;i<n;i++)
{
for(j=;j<m;j++){
if(arr[i][j]=='.'){
start_i=i;
start_j=j;
// cout<<i<<"_"<<j<<endl;
break;
}
}
if(j!=m)
break;
}
ans=;
memset(mark,,sizeof(mark));
mark[start_i][start_j]=;
dfs(start_i,start_j,);
memset(mark,,sizeof(mark));
dfs(xx,yy,);
printf("Maximum rope length is %d.\n",ans);
} return ;
}
 
 

Labyrinth 树的直径加DFS的更多相关文章

  1. 大臣的旅费---树的直径(dfs)

    很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首都直接或者 ...

  2. Codeforces 592D - Super M - [树的直径][DFS]

    Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...

  3. POJ 1985 Cow Marathon (树形DP,树的直径)

    题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...

  4. HDU 4123 Bob’s Race 树的直径+单调队列

    题意: 给定n个点的带边权树Q个询问. 以下n-1行给出树 以下Q行每行一个数字表示询问. 首先求出dp[N] :dp[i]表示i点距离树上最远点的距离 询问u, 表示求出 dp 数组中最长的连续序列 ...

  5. NOI 2003 逃学的小孩 (树的直径)

    [NOI2003 逃学的小孩] 题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:"喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?"一 ...

  6. 【BZOJ-1912】patrol巡逻 树的直径 + DFS(树形DP)

    1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1034  Solved: 562[Submit][St ...

  7. 历届试题 大臣的旅费-(树的直径+dfs)

    问题描述 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首 ...

  8. POJ 1383 Labyrinth (bfs 树的直径)

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

  9. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

随机推荐

  1. 运行docker大致流程

    平时部署测试环境使用jenkins将代码打包成docker镜像部署在rancher中,闲下来研究了一下docker的大致流程,自己画了一个流程图

  2. display:flex 简单记录

    1.有写了 display:flex:这个就是 采用了 flex布局的 元素 这个元素可以 写 6个属性: flex-direction : row |  column  | row-reverse ...

  3. tigervnc使用总结

    vncserver和x0vncserver用法总计 通常vncserver :port 会调用到xvnc,这时系统会新建一个虚拟桌面通过vncserver分享出去. vncserver的用法很简单: ...

  4. OpenCV-Python 理解SVM | 五十五

    目标 在这一章中 我们将对SVM有一个直观的了解 理论 线性可分数据 考虑下面的图像,它具有两种数据类型,红色和蓝色.在kNN中,对于测试数据,我们用来测量其与所有训练样本的距离,并以最小的距离作为样 ...

  5. PHP7内核:源码分析的环境与工具

    本文主要介绍分析源码的方式,其中包含环境的搭建.分析工具的安装以及源码调试的基本操作. 一.工具清单 PHP7.0.12 GDB CLion 二.源码下载及安装 $ wget http://php.n ...

  6. sql 数据库操作语句 不带select

    MySQL数据操作语句 1.总纲 DDL -数据定义语句** create/drop/alter ** create: 创建 drop:删除 alter:修改 DML -数据操作语句 ** inser ...

  7. 使用docsify 写开源文档

    使用docsify 写开源文档 官网:https://docsify.js.org/#/ docsify 是一个动态生成文档网站的工具.不同于 GitBook.Hexo 的地方是它不会生成将 .md ...

  8. css 动画 transition和animation

    本文参考:http://www.ruanyifeng.com/blog/2014/02/css_transition_and_animation.html 1. transition基本用法: < ...

  9. 算法(algorithm)

    算法是什么? 算法是指令的集合,是为解决特定问题而规定的一系列操作. 它是明确定义的可计算过程,以一个数据集合作为输入,并产生一个数据集合作为输出. 一个算法通常来说具有以下五个特性: 1.输入:一个 ...

  10. Linux yum 源配置

    CentOS 7 使用 163 的 yum 源,配置步骤如下: 下载镜像源文件 http://mirrors.163.com/.help/centos.html 备份原配置文件,将下载的文件的名字改成 ...