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. php版给UEditor的图片在线管理栏目增加图片删除功能

    1.找到uedior/dialogs/image/image.js文件,Add为修改部分的代码: /** * tab点击处理事件 * @param tabHeads * @param tabBodys ...

  2. 原型设计工具 Axure RP 7.0下载地址及安装说明

    Axure RP是产品经理必备的原型制作工具,因为很多同学是新手,在这里整理一下axure7.0的下载.安装和汉化流程,希望能够帮到大家. Axure RP是美国Axure Software Solu ...

  3. FFmpeg编译i386 OSX 脚本

    话不多说, 直接上脚本 #!/bin/sh # directories PLATFORM="OSX" # FFmpeg脚本目录 SOURCE="ffmpeg-2.8.7& ...

  4. 上机练习2 生成计算机ID

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  5. android打开文件、保存对话框、创建新文件夹对话框(转载)

    转载地址:点击打开 这是一个简单的只有3个按钮的程序,3个按钮分别对应三种工作的模式(保存.打开和文件夹选择).封装的SimpleFileDialog.java的内容如下: package com.e ...

  6. java 基础 02 数据类型、运算符、分支结构

    内容: (1)数据类型 (2)运算符 (3)分支结构 1.数据类型 java语言中的基本数据类型:byte.short.int.long.float.double.boolean.char. 1.1布 ...

  7. HttpURLConnection(http 1.1) 用法、状态码、状态描述

    最近研究了java的HttpURLConnection的用法, 这里简单的做一下记录: Java中可以使用HttpURLConnection来请求WEB资源. 1. URL请求的类别 分为二类,GET ...

  8. PHP函数:mysql_fetch_assoc指针重置

    本文目前主要讨论mysql_fetch_assoc“指针”如何重置的问题 要了解mysql_fetch_assoc,先看看它与mysql_fetch_row和mysql_fetch_array的关系. ...

  9. SwiftHN阅读器应用IOS源码

    SwiftHN是用Swift语言编写的Hacker News阅读器,同时采用了iOS 8最新的API. <ignore_js_op> <ignore_js_op> 详细说明:h ...

  10. hdu-1162 Eddy's picture---浮点数的MST

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1162 题目大意: 给n个点,求MST权值 解题思路: 直接prim算法 #include<bi ...