[POJ1383]Labyrinth

试题描述

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.

输入

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.

输出

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.

输入示例


###
#.#
### #######
#.#.###
#.#.###
#.#.#.#
#.....#
#######

输出示例

Maximum rope length is .
Maximum rope length is .

数据规模及约定

见“输入

题解

注意到题目中说每两个空地之间只会有一条路径相连,所以整张地图是一个树的结构,找树的直径即可。

方法:随便找一个空地开始 BFS,找到最远的点 a,再找 a 最远的点 b,则路径 a~b 即为直径。(相关证明请查阅互联网)(或者用树形 dp 也能求)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 1010
int n, m;
char Map[maxn][maxn];
struct Point {
int x, y;
Point(): x(0), y(0) {}
Point(int _, int __): x(_), y(__) {}
} ; queue <Point> Q;
int step[maxn][maxn], dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};
Point BFS(Point s) {
memset(step, -1, sizeof(step));
step[s.x][s.y] = 0;
Q.push(s);
Point ans(-1, 0);
while(!Q.empty()) {
Point u = Q.front(); Q.pop();
for(int d = 0; d < 4; d++) {
Point v(u.x + dx[d], u.y + dy[d]);
if(1 <= v.x && v.x <= n && 1 <= v.y && v.y <= m && Map[v.x][v.y] == '.' && step[v.x][v.y] < 0) {
step[v.x][v.y] = step[u.x][u.y] + 1;
Q.push(v);
}
}
if(ans.x < 0 || step[ans.x][ans.y] < step[u.x][u.y]) ans = u;
}
return ans;
} int main() {
int T = read();
while(T--) {
memset(Map, 0, sizeof(Map));
n = read(); m = read();
swap(m, n);
Point s(-1, 0);
for(int i = 1; i <= n; i++) {
scanf("%s", Map[i] + 1);
for(int j = 1; j <= m; j++)
if(Map[i][j] == '.' && s.x == -1) s = Point(i, j);
} Point tmp = BFS(BFS(s));
printf("Maximum rope length is %d.\n", step[tmp.x][tmp.y]);
} return 0;
}

[POJ1383]Labyrinth的更多相关文章

  1. poj分类解题报告索引

    图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...

  2. 2014百度之星资格赛 1004:Labyrinth(DP)

    Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. ural 1145. Rope in the Labyrinth

    1145. Rope in the Labyrinth Time limit: 0.5 secondMemory limit: 64 MB A labyrinth with rectangular f ...

  4. timus 1033 Labyrinth(BFS)

    Labyrinth Time limit: 1.0 secondMemory limit: 64 MB Administration of the labyrinth has decided to s ...

  5. poj 1383 Labyrinth

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

  6. Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集

    C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...

  7. 2014年百度之星程序设计大赛 - 资格赛 1004 Labyrinth(Dp)

    题目链接 题目: Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

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

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

  9. 2014百度之星第四题Labyrinth(DP)

    Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. 每天一个linux命令(9):nl命令

    nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...

  2. 某表含有N个字段超精简模糊查询方法

    我们在做多个字段模糊查询时,是不是觉得非常麻烦?比如我要模糊查询某表多个字段存在某数据时,如下 select * from table where a like '%key%' or b  like ...

  3. Task.Run Vs Task.Factory.StartNew

    在.Net 4中,Task.Factory.StartNew是启动一个新Task的首选方法.它有很多重载方法,使它在具体使用当中可以非常灵活,通过设置可选参数,可以传递任意状态,取消任务继续执行,甚至 ...

  4. [转载]ODAC (odp.net) 开发到部署

    1. 确定你开发机和服务器的操作系统是32位还是64位, 而且要确定要部署的服务器是什么操作系统; 2. 下载开发机和服务器所需的dll, 地址:http://download.csdn.net/de ...

  5. Java设计模式-观察者模式(Observer)

    包括这个模式在内的接下来的四个模式,都是类和类之间的关系,不涉及到继承,学的时候应该 记得归纳,记得本文最开始的那个图.观察者模式很好理解,类似于邮件订阅和RSS订阅,当我们浏览一些博客或wiki时, ...

  6. Day4_计算器

    read me 1.构造三个函数,乘除(mad),加减(aas),去括号(par): 2.获取表达式字符串之后,判断是否包含“+-*/()”等字符,包含则下一步3:不包含,返回字符串: 3.par 函 ...

  7. html转jsp乱码问题

    先由html后缀转为jsp后缀.然后添加 <%@ page language="java" import="java.util.*" pageEncodi ...

  8. TypeError: 'module' object is not callable cp fromhttp://blog.csdn.net/huang9012/article/details/17417133

    程序代码  class Person:      #constructor      def __init__(self,name,sex):           self.Name = name   ...

  9. 安卓系统源码编译系列(六)——单独编译内置浏览器WebView教程

    原文                   http://blog.csdn.net/zhaoxy_thu/article/details/18883015                 本文主要对从 ...

  10. Git Pull 避免用户名和密码方法

    在开发中使用的版本控制器时git , 每次使用命令"git pull"从服务器获得最新代码时,都需要输入用户名和密码,这样浪费了大量的时间和热情,在此背景下,本文在网上找到解决版本 ...