博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[POJ1383]Labyrinth
阅读量:5348 次
发布时间:2019-06-15

本文共 3702 字,大约阅读时间需要 12 分钟。

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

输入示例

23 3####.####7 6########.#.####.#.####.#.#.##.....########

输出示例

Maximum rope length is 0.Maximum rope length is 8.

数据规模及约定

见“输入

题解

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

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

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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 1010int n, m;char Map[maxn][maxn];struct Point { int x, y; Point(): x(0), y(0) {} Point(int _, int __): x(_), y(__) {}} ;queue
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;}

 

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5803861.html

你可能感兴趣的文章
mysql统计一张表中条目个数的方法
查看>>
ArcGIS多面体(multipatch)解析——引
查看>>
css3渐变画斜线 demo
查看>>
JS性能DOM优化
查看>>
HAL层三类函数及其作用
查看>>
Odoo 去掉 恼人的 "上午"和"下午"
查看>>
web@h,c小总结
查看>>
java编程思想笔记(一)——面向对象导论
查看>>
Data Structure 基本概念
查看>>
Ubuntu改坏sudoers后无法使用sudo的解决办法
查看>>
NEYC 2017 游记
查看>>
[搬运] 写给 C# 开发人员的函数式编程
查看>>
Python之旅Day14 JQuery部分
查看>>
core--线程池
查看>>
redux-effect
查看>>
Android轻量级的开源缓存框架ASimpleCache
查看>>
他山之石:加载图片的一个小问题
查看>>
shell - 常识
查看>>
mssql sqlserver 使用sql脚本 清空所有数据库表数据的方法分享
查看>>
分层图最短路【bzoj2763】: [JLOI2011]飞行路线
查看>>