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

AC代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int c,r,dx,dy;
int ans;
char a[1005][1005];
int b[1005][1005];
int mx[4] = {0,0,1,-1};
int my[4] = {1,-1,0,0};
void dfs(int x,int y,int t)
{
b[x][y]=1;
if(ans<t)
{
dx=x;
dy=y;
ans=t;
}
int xx,yy,i;
for(i=0;i<4;i++)
{
xx=x+mx[i];
yy=y+my[i];
if(xx>-1&&xx<r&&yy>-1&&yy<c&&!b[xx][yy]&&a[xx][yy]!='#')
dfs(xx,yy,t+1);
}
b[x][y]=1;
}
int main()
{
int t,i,j,sx,sy;
scanf("%d",&t);
while(t--)
{
int l=0;
ans=0;
memset(b,0,sizeof(b));
scanf("%d %d",&c,&r);
for(i=0;i<r;i++)
{
scanf("%s",a[i]);
for(j=0;j<c&&l==0;j++)
if(a[i][j]=='.')
{
l=1;
sx=i;
sy=j;
}
}
dfs(sx,sy,0);
memset(b,0,sizeof(b));
dfs(dx,dy,0);
printf("Maximum rope length is %d.\n",ans);
}
}

  

我的代码:

 1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #include<algorithm>
5
6 using namespace std;
7
8 int r, c;
9 char a[1005][1005];
10 int num[1005][1005];
11 int n_step, max_step, nx, ny;
12 int dx[4] = {0, 1, 0, -1};
13 int dy[4] = {1, 0, -1, 0};
14
15 int dfs(int x, int y, int step)
16 {
17 n_step = step;
18 a[x][y] = '#';
19 for(int i = -1; i <= 1; i++)
20 {
21
22 max_step = max(max_step, n_step);
23 nx = x + dx[i];
24 ny = y + dy[i];
25 if(nx > 0 && nx <= r && ny > 0 && ny <= c && a[nx][ny] == '.')
26 {
27 n_step++;
28 dfs(nx, ny, n_step);
29 }
30 }
31 return max_step;
32 }
33
34 int main()
35 {
36 int t;
37
38 scanf("%d", &t);
39 while(t--)
40 {
41 n_step = 0;
42 max_step = 0;
43 scanf("%d %d", &r, &c);
44 getchar();
45 memset(num, 0, sizeof(num));
46 for(int i = 0; i < max(r, c) + 1; i++)
47 {
48 a[0][i] = '#';
49 a[i][0] = '#';
50 a[r+1][i] = '#';
51 a[i][c+1] = '#';
52 }
53
54 //cout << r << c <<endl;
55 for(int i = 1; i <= r; i++)
56 {
57 for(int j = 1; j <= c; j++)
58 {
59 scanf("%c", &a[i][j]);
60 }
61 getchar();
62 }
63
64 for(int i = 1; i <= r; i++)
65 {
66 for(int j = 1; j <= c; j++)
67 {
68 if(a[i][j] == '.')
69 {
70 //cout << "++" << i << j << endl;
71 int ans = dfs(i, j, 0);
72 printf("%d\n", ans);
73 break;
74 }
75 }
76 }
77
78
79 }
80 return 0;
81 }

I - 树的直径 POJ - 1383的更多相关文章

  1. 树的直径 poj 2631

    树的直径:从随意一点出发,BFS找到最远的距离,然后在从该点出发BFS找到最远的距离 #include <iostream> #include <algorithm> #inc ...

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

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

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

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

  4. POJ 1383题解(树的直径)(BFS)

    题面 Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4997 Accepted: 1861 Descript ...

  5. 树的最长链-POJ 1985 树的直径(最长链)+牛客小白月赛6-桃花

    求树直径的方法在此转载一下大佬们的分析: 可以随便选择一个点开始进行bfs或者dfs,从而找到离该点最远的那个点(可以证明,离树上任意一点最远的点一定是树的某条直径的两端点之一:树的直径:树上的最长简 ...

  6. poj 1985 Cow Marathon 树的直径

    题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...

  7. POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)

    树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...

  8. POJ 2631 Roads in the North(树的直径)

    POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...

  9. POJ 1985 Cow Marathon(树的直径模板)

    http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...

随机推荐

  1. 浮动引发的高度塌陷问题及其解决方法(BFC相关概念及性质)

    浮动引发的高度塌陷问题 高度塌陷问题的产生 BFC(Block Formatting Context)的引入 元素开启BFC后的特点 开启BFC的元素不会被其他浮动元素所覆盖 开启BFC的元素不会发生 ...

  2. 后端程序员之路 34、Index搜索引擎实现分析3-对文章索引的两层分块

    # part_indexer 对文章根据id的hash进行分块索引- 持有 search_index _inc_index[2]; search_index _history_index[2]; 进行 ...

  3. Jenkins+Python自动化测试持续集成详细教程

    ​ Jenkins是一个开源的软件项目,是基于java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能.由于是基于java开发因此它也依赖jav ...

  4. 权益满满 | PGConf.Asia2020大会诚邀赞助商!携手走向更大世界!

    转: 权益满满 | PGConf.Asia2020大会诚邀赞助商!携手走向更大世界! 关于PostgresConf.CN&PGConf.Asia2020大会作为全球最大.最知名的PG序列会议, ...

  5. 如何在 C# 8 中使用 Channels

    在面对 生产者-消费者 的场景下, netcore 提供了一个新的命名空间 System.Threading.Channels 来帮助我们更高效的处理此类问题,有了这个 Channels 存在, 生产 ...

  6. 翻译:《实用的Python编程》05_01_Dicts_revisited

    目录 | 上一节 (4.4 异常) | 下一节 (5.2 封装) 5.1 再谈字典 Python 对象系统主要基于字典实现.本节将对此进行讨论. 字典 字典是命名值(named values)的集合. ...

  7. 使用Java+NetBeans设计web服务和页面,用Tomcat部署网页

    一 安装NetBeans(自动安装jdk) 二 创建服务器 三 发布服务 一 安装NetBeans(自动安装jdk) 进入oracle的下载界面: http://www.oracle.com/tech ...

  8. 记录Java注解在JavaWeb中的一个应用实例

    概述 在学习注解的时候,学了个懵懵懂懂.学了JavaWeb之后,在做Demo项目的过程中,借助注解和反射实现了对页面按钮的权限控制,对于注解才算咂摸出了点味儿来. 需求 以"角色列表&quo ...

  9. OpenCV图像处理中的“机器学习"技术的使用

    注意,本文中所指"机器学习"(ML)技术,特指SVM.随机森林等"传统"技术. 一.应用场景        相比较当下发展迅速的各路"端到端" ...

  10. 封装一个postMessage库,进行iframe跨域交互

    这是近期个人在开发chrome插件时的其中一个小总结.还有很多没有总结出来.因为目前插件还在迭代中,(herry菌插件,用于B站C站),属于个人业余的一个小项目.还有很多功能没有实现,以及还需要再看能 ...