[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.
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;
}