Labyrinth POJ - 1383

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
 
 
题意:由 ' . '构成的最长路
题解:树的直径
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=1e3+;
const int INF=0x3f3f3f3f;
int n,m,ans;
char map[maxn][maxn];
int dis[maxn][maxn];
int dx[] = {,-,,};
int dy[] = {,,,-};
struct node{
int x,y;
};
node second;
void bfs(node first)
{
ans = ;
memset(dis,-,sizeof dis);
queue<node> que;
node p = {first.x,first.y};
dis[first.x][first.y] = ;
que.push(p);
while(!que.empty())
{
node tmp = que.front();
que.pop();
for(int i=; i<; i++)
{
int nx = tmp.x + dx[i];
int ny = tmp.y + dy[i];
if(nx>= && nx < m && ny>= && ny<n && map[nx][ny] != '#' && dis[nx][ny] == -)
{
node Next;
Next.x = nx;
Next.y = ny;
que.push(Next);
dis[nx][ny] = dis[tmp.x][tmp.y] + ;
if(ans < dis[nx][ny])
{
ans = dis[nx][ny];
second.x = nx;
second.y = ny;
}
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
struct node first;
scanf("%d %d",&n,&m);
getchar();
for(int i=; i<m; i++)
scanf("%s",map[i]);
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
if(map[i][j] == '.')
{
first.x = i;
first.y = j;
}
}
bfs(first);
bfs(second);
printf("Maximum rope length is %d.\n",ans);
}
}

Labyrinth POJ - 1383的更多相关文章

  1. poj 1383 Labyrinth

    题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...

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

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

  3. POJ 1383 Labyrinth (树的直径求两点间最大距离)

    Description The northern part of the Pyramid contains a very large and complicated labyrinth. The la ...

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

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

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

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

  6. I - 树的直径 POJ - 1383

    The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is d ...

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

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

  8. 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分

    树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...

  9. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

随机推荐

  1. linux下mysql-5.5.27.tar.gz源程序包安装实例

    研究了好几天,终于把mysql装上了,现在来做下小结. 系统环境:fedora8 虚拟机. 1.检查安装使用的编译工具gcc是否存在,如果不存在则要下载安装 # gcc -v 2.卸载低版本的mysq ...

  2. zookeeper的简单搭建,java使用zk的例子和一些坑

    一 整合 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的github项目地址 https://github.com/247292980/spring- ...

  3. 记DotNetBar换肤

    界面: comboBoxEx  选择皮肤 buttonX 测试指定皮肤 styleManager 后台代码: 初始化 : this.EnableGlass = false; 设置窗体效果 不设置 依然 ...

  4. 19.CentOS7下PostgreSQL安装过程

    CentOS7下PostgreSQL安装过程 装包 sudo yum install postgresql-server postgresql-contrib 说明: 这种方式直接明了,其他方法也可以 ...

  5. 在Magento中用MySQL模拟队列发送电子邮件

    1. 需求 顾客在网站上购买特定商品并且这些商品的总金额超过特定金额后,使用email给顾客发送一个优惠券:假如某件商品已经降价了,则此商品的金额不计算在目标总金额内: 2. 需求分析 ①发送优惠券的 ...

  6. css3的transform变换scale和translate等影响jQuery的position().top和offset().top

    css3的transform变换scale和translate等影响jQuery的position().top和offset().top

  7. js之BOM和DOM

      今天我们来学习js中的一些基础的操作. 一.BOM对象 BOM(浏览器对象模型),可以对浏览器窗口进行访问和操作.使用 BOM,开发者可以移动窗口.改变状态栏中的文本以及执行其他与页面内容不直接相 ...

  8. jQuery遍历Table表格的行和列

    遍历Table表格的行和列,在开发中比较常用的功能,特别是前端开发人员,不多说,直接上代码,下面代码只是弹出第一列字段,请各位自己根据需求修改和扩展! <!DOCTYPE html PUBLIC ...

  9. nmon 工具的使用

    原文链接:https://www.ibm.com/developerworks/cn/aix/library/analyze_aix/ 引言 nmon 工具可以为 AIX 和 Linux 性能专家提供 ...

  10. Apache Spark 2.2.0 中文文档 - GraphX Programming Guide | ApacheCN

    GraphX Programming Guide 概述 入门 属性 Graph 示例属性 Graph Graph 运算符 运算符的汇总表 Property 运算符 Structural 运算符 Joi ...