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. NLP interview

    2019-08-26 17:19:58 1)聊实习项目 2)代码题,二维数组中的查找某个target 3)讲一些最能体现创新能力的工作,而不是一些工程上的实现 4)讲论文可以从哪些方面做创新点,文本生 ...

  2. Manjaro更新后 搜狗拼音输入法突然无法正常使用

    之前Manjaro已经用了很久了,很多该配置的都已经配置好了,但是搜狗拼音在系统更新后突然无法使用 1检查 如下依赖 2.检查配置文件 3.发现一切配置没问题,此时输入 sogou-qimpanel ...

  3. CMD 基础命令

    基本命令 1.编译.java文件成.class:找到文件所在路径 --> java -d . 文件名称.java --> javac -d . 文件名称.java : 2.ping URL ...

  4. 如何删除Python中文本文件的文件内容?

    在python中: open('file.txt', 'w').close() 或者,如果你已经打开了一个文件: f = open('file.txt', 'r+') f.truncate(0) # ...

  5. Sprinboot 整合 RabbitMQ,RabbitMQ 消息重试机制

    当消费者消费消息的时候,出现错误,RabbitMQ 本身会有

  6. LVS的部署、案例、以及常见问题

    LVS的部署.案例.以及常见问题 原创chenhuyang 最后发布于2018-06-03 16:18:25 阅读数 1560 收藏 展开 一.LVS的部署 LVS现在已经集成在linux内核模块中, ...

  7. html vue简单

    1.Vue 简单的替换更新 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  8. Android进阶AIDL使用自定义类型

    原文首发于微信公众号:jzman-blog,欢迎关注交流! 上篇文章中主要介绍从 AIDL 的使用方式以及 Android 开发中不同进程之间的通信,遗留的问题是如何在 AIDL 中使用自定义类型,具 ...

  9. 逃生 HDU 4857(反向建图 + 拓扑排序)

    逃生 链接 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必 ...

  10. Debug 是门艺术

    最近想结合发生在身边码农身上的一些小故事,尝试表达一个观点“Coding 是门技术,Debug 是门艺术”. 上期的分享<Coding 是门技术>主要通过引入身边 Code farmer ...