[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. 让Dapper+SqlCE支持ntext数据类型和超过4000字符的存储

    使用Dapper和SqlCE进行开发的时候,如果数据库的某字段是采用的ntext数据类型,并且在这个字段存储的数据超过了4000个字符,会报如下的错误: Invalid parameter Size ...

  2. [wikioi2069]油画(贪心)

    题目:http://www.wikioi.com/problem/2069/ 分析: 首先这个问题比较复杂,涉及到两个重要的考虑点,一个是当前拿来的颜色是否保留,一个是若保留后那么应该把当前盘子的哪个 ...

  3. javascript基础(1)

    1.前言 ECMAscript解释,用来解释JS代码 DOM 文档对象模型,浏览器在做显示时需要渲染DOM树 BOM  浏览器对象模型,可以控制浏览器的行为,代码的兼容性很差 2.基本类型 数字类型: ...

  4. 每天一个linux命令(47):traceroute命令

    通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...

  5. C语言和数据结构的书单-再次推荐

    一.推荐专业书单: 1)         C语言方面: n  明解C语言——适合初学者 豆瓣链接:https://book.douban.com/subject/23779374/ 推荐理由:< ...

  6. nginx location的配置

    文章转自:http://www.ttlsa.com/nginx/nginx-location-configure/ location的语法配置规则: 语法规则: location [=|~|~*|^~ ...

  7. Java基础-静态代理与动态代理比较

    JAVA的静态代理与动态代理比较 静态代理类: 由程序员创建或由特定工具自动生成源代码,再对其编译.在程序运行前,代理类的.class文件就已经存在了.动态代理类: 在程序运行时,运用反射机制动态创建 ...

  8. Tomcat 部署

    <CATALINA_HOME>/webapps: Tomcat的主要Web发布目录,默认情况下把Web应用文件放于此目录. 1.war包部署:  将需要发布的web应用打成war文件, ( ...

  9. 理解SIFT

    理解SIFT.tab{font-size:12px; margin-bottom: 10px;}.tab a{cursor:pointer;cursor:pointer;display:inline- ...

  10. BZOJ-1067 降雨量 线段树+分类讨论

    这道B题,刚的不行,各种碎点及其容易忽略,受不鸟了直接 1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2859 ...