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. 介绍 Seq2Seq 模型

    2019-09-10 19:29:26 问题描述:什么是Seq2Seq模型?Seq2Seq模型在解码时有哪些常用办法? 问题求解: Seq2Seq模型是将一个序列信号,通过编码解码生成一个新的序列信号 ...

  2. Building Applications with Force.com and VisualForce(Dev401)(十四):Implementing Business Processes:Auditing Processes

    Dev401-015:Implementing Business Processes:Auditing Processes Module Objectives1.list some of the fe ...

  3. 在django中如何从零开始搭建一个mock服务

    mock概念 mock 就是模拟接口返回的一系列数据,用自定义的数据替换接口实际需要返回的数据,通过自定义的数据来实现对下级接口模块的测试.这里分为两类测试:一类是前端对接口的mock,一类是后端单元 ...

  4. OpenCV-Python 理解特征 | 三十六

    目标 在本章中,我们将尝试理解什么是特征,为什么拐角重要等等 解释 你们大多数人都会玩拼图游戏.你会得到很多小图像,需要正确组装它们以形成大的真实图像.问题是,你怎么做?将相同的理论投影到计算机程序上 ...

  5. iOS 内存分配与分区

    RAM ROM RAM:运行内存,不能掉电存储. ROM:存储性内存,可以掉电存储,例如内存卡.Flash. 由于 RAM 类型不具备掉电存储能力(即一掉电数据消失),所以 app 程序一般存放于 R ...

  6. 医学图像分割-在3DSlicer中使用英伟达的AI辅助工具NvidiaAIAssistedAnnotation自动切割医学图像教程

    前期准备 下载3DSlicer并安装: 3DSlicer官网(http://www.slicer.org)或者直接下载(https://download.slicer.org),需要注意目前该插件只支 ...

  7. jQuery学习笔记01

    1.jQuery介绍 1.1什么是jQuery ? jQuery,顾名思义,也就是JavaScript和查询(Query),它就是辅助JavaScript开发的js类库. 1.2 jQuery核心思想 ...

  8. 3.26java作业

    1.编写程序, 输入变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none.(知识点:if条件语句) package fda; imp ...

  9. session分布式处理

    session分布式处理 标签(空格分隔): 分布式 1. Session复制 在支持Session复制的Web服务器上, 通过修改服务器配置, 可以实现将Session同步到其它Web服务器上, 达 ...

  10. 多线程之旅(Task 任务)

    一.Task(任务)和ThreadPool(线程池)不同       源码 1.线程(Thread)是创建并发工具的底层类,但是在前几篇文章中我们介绍了Thread的特点,和实例.可以很明显发现局限性 ...