[POJ1383]Labyrinth
[POJ1383]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.
输出
输入示例
###
#.#
### #######
#.#.###
#.#.###
#.#.#.#
#.....#
#######
输出示例
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的更多相关文章
- poj分类解题报告索引
图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...
- 2014百度之星资格赛 1004:Labyrinth(DP)
Labyrinth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- ural 1145. Rope in the Labyrinth
1145. Rope in the Labyrinth Time limit: 0.5 secondMemory limit: 64 MB A labyrinth with rectangular f ...
- timus 1033 Labyrinth(BFS)
Labyrinth Time limit: 1.0 secondMemory limit: 64 MB Administration of the labyrinth has decided to s ...
- poj 1383 Labyrinth
题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...
- 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 ...
- 2014年百度之星程序设计大赛 - 资格赛 1004 Labyrinth(Dp)
题目链接 题目: Labyrinth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- poj 1383 Labyrinth【迷宫bfs+树的直径】
Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4004 Accepted: 1504 Descrip ...
- 2014百度之星第四题Labyrinth(DP)
Labyrinth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- nginx自学
需要了解的linux的命令: linux的命令:netstat -antnetstat -antp(天假了参数P)ps aux | grep 80kill -9 2985 号进程pkill -9 ht ...
- DOM(五)事件对象
浏览器中的事件都是以对象的形式存在的,同样ie浏览器与标准dom浏览器之间存在获取事件对象上也存在差别.在ie浏览器中事件对象是windows对象的一个属性event,访问通常采用如下方法. oP.o ...
- SQL删除重复的记录(只保留一条)
首先新建表: --创建示例表 CREATE TABLE t ( id ,) PRIMARY KEY, a ), b ) ) --插入数据 INSERT INTO t SELECT 'aa','bb' ...
- 每天一个linux命令(50):telnet命令
telnet 命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是 Internet远程登陆服务的标准协议和主要方式.它为 ...
- Centos下apache启动时httpd: apr_sockaddr_info_get() failed for 报错
今天安装Apache httpd web服务器时,从官方网站上http://www.apache.org/dyn/closer.cgi下载httpd,然后在centos下解压,安装过程分为三部分: ( ...
- Spring-如何实现事物管理的
事务的实现方式 实现方式共有两种:编码方式:声明式事务管理方式.基于AOP技术实现的声明式事务管理,实质就是:在方法执行前后进行拦截,然后在目标方法开始之前创建并加入事务,执行完目标方法后根据执行情况 ...
- 【CodeForces 620D】Professor GukiZ and Two Arrays
题 题意 两个数列,一个有n个数,另一个有m个数,让你最多交换两次两个数列的数,使得两个数列和的差的绝对值最小,求这个差的绝对值.最少交换次数.交换数对 分析 交换0次.1次可得到的最小的差可以枚举出 ...
- 【CodeForces 621C】Wet Shark and Flowers
题 There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such tha ...
- 洛谷P1363 幻想迷宫
题目描述 背景 Background (喵星人LHX和WD同心协力击退了汪星人的入侵,不幸的是,汪星人撤退之前给它们制造了一片幻象迷宫.) WD:呜呜,肿么办啊…… LHX:momo...我们一定能走 ...
- TYVJ1939 玉蟾宫
背景 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. 描述 这片土地被分成N*M个格子,每个格子里写着'R'或者'F',R代 ...