[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 ...
随机推荐
- 用css画出对话框
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa4AAAFSCAYAAACqpTv4AAAgAElEQVR4nO3deZBU5b3GcUIlVTG3bi
- 利用less监视模式实时预览样式刷新浏览器
[前言]此处介绍的方法只是我个人的用法,相信大家有更好更简洁的方式. 上次写到利用LiveReload解放F5.而且LiveReload可以编辑sass/less/stylus.但是可惜发现LiveR ...
- Owin中间件搭建OAuth2.0认证授权服务体会
继两篇转载的Owin搭建OAuth 2.0的文章,使用Owin中间件搭建OAuth2.0认证授权服务器和理解OAuth 2.0之后,我想把最近整理的资料做一下总结. 前两篇主要是介绍概念和一个基本的D ...
- 知道了grunt怎么用了
第一步: //如何安装指定grunt的插件,安装后自动在当前cmd目录下新建node_modules文件夹,文件夹里面包含了下载完成的插件 npm install <module> - ...
- JS高级设计第七章——复习知识点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java设计模式-适配器模式(Adapter)
适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题.主要分为三类:类的适配器模式.对象的适配器模式.接口的适配器模式.首先,我们来看看类的适配器模 ...
- sql-exists和not exists
EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或FalseEXISTS 指定一个子查询,检测行的存在. 实例: (一). 在子查询中使用 NULL ...
- 【POJ 1260】Pearls
题 题意 有n个(n≤100)等级的珍珠,等级越高单价越高,要购买一种等级的珍珠就要多付10*单价,现在需要购买一些等级的珍珠一定数量,若买更高等级的珍珠更便宜则可以买更高等级的珍珠,求最少花费. 分 ...
- Yii2分页
Yii中的分页功能主要由yii\web: Linkable接口.yii\widgets: LinkPager类和yii\data: Pagination类三个组成 yii\data: Paginati ...
- ubuntu14.10建立热点wifi分享给手机
http://jingyan.baidu.com/article/363872ecd8f35d6e4ba16f97.html ubuntu14.10建立热点wifi分享给手机