POJ 1383题解(树的直径)(BFS)
题面
Labyrinth
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 4997 Accepted: 1861
Description
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.
Input
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.
Output
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.
Sample Input
2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######
Sample Output
Maximum rope length is 0.
Maximum rope length is 8.
Hint
Huge input, scanf is recommended.
If you use recursion, maybe stack overflow. and now C++/c ‘s stack size is larger than G++/gcc
Source
Central Europe 1999
题目大意:在迷宫中找出最长的一条简单路径,求路径长度
分析:
此题为树的直径模板题
树上最长的简单路径即为树的直径。
求树的直径的方法就是在树上任选一点u,求距离点u最远的点y,再求距离点y最远的点s,点y到点s的距离即为树的直径。
原理: 设起点为u,第一次BFS找到的终点v一定是树的直径的一个端点
证明:
(1) 如果u 是直径上的点,则v显然是直径的终点
(如果v不是的话,假设直径起点为a,终点为b,又因为u到b的距离小于u到v的距离,那么路径a->u->v比直径更长,这与直径的定义矛盾)
(2) 如果u不是直径上的点,则u到v必然于树的直径相交(反证法),那么交点到v 必然就是直径的一部分
综上所述,v一定是直径的一个端点,所以从v进行BFS得到的一定是直径长度
更详细的证明可以参考http://blog.sina.com.cn/s/blog_dbe928200101cm5t.html
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 1005
using namespace std;
int n,m,t;
char a[maxn][maxn];
int used[maxn][maxn];//判重
const int walkx[4]={1,-1,0,0},walky[4]={0,0,1,-1};//控制走的方向
struct node{//广搜用结构体
int x;
int y;
int t;
node(){}
node(int xx,int yy,int tim){
x=xx;
y=yy;
t=tim;
}
};
int is_check(int x,int y){//判断下一步是否能走
if(x<=0||y<=0||x>n||y>m||used[x][y]==1||a[x][y]=='#') return 0;
else return 1;
}
node bfs(int sx,int sy){//sx,sy为起点
memset(used,0,sizeof(used));
queue<node>q;
q.push(node(sx,sy,0));
node now,nex;
int tx,ty;
int maxt=0,maxx=sx,maxy=sy;
while(!q.empty()){
now=q.front();
q.pop();
if(now.t>maxt){//记录距离最大值
maxx=now.x;
maxy=now.y;
maxt=now.t;
}
for(int i=0;i<4;i++){
tx=now.x+walkx[i];
ty=now.y+walky[i];
if(is_check(tx,ty)){
used[tx][ty]=1;
q.push(node(tx,ty,now.t+1));
}
}
}
return node(maxx,maxy,maxt);//返回结构体,可以知道距离任选的一点u最远的点v的位置,方便下次广搜
}
int main(){
char tmp[maxn];
scanf("%d",&t);
while(t--){
scanf("%d %d\n",&n,&m);
swap(n,m);
int xx,yy;
xx=yy=-1;
for(int i=1;i<=n;i++){
scanf("%s",tmp+1);
for(int j=1;j<=m;j++){
a[i][j]=tmp[j];
if(a[i][j]=='.'&&xx==-1&&yy==-1){
xx=i;//随便选一个点作为起点
yy=j;
}
}
// getchar();
}
node tmp1=bfs(xx,yy);
node tmp2=bfs(tmp1.x,tmp1.y);
printf("Maximum rope length is %d.\n",tmp2.t);
}
}
POJ 1383题解(树的直径)(BFS)的更多相关文章
- poj2631 树的直径 + bfs
//Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iost ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- hdu2196 树的直径 + bfs
//Accepted 740 KB 15 ms //树的直径 //距离一个顶点最远的点一定是树的直径的一个端点 #include <cstdio> #include <cstring ...
- 树上选两点(使最短)树的直径+bfs
题意: 给你一颗树,让你放两个点,放在哪里的时候任意点到某个最近的消防站最远值最小. 思路: 树的直径类题目. 首先我们想两个点会把整棵树分成两个团,所以肯定会在树的某个链上切开. 而且要切一定切在树 ...
- ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS
题意:给一棵树,要求找出两个点,使得所有点到这两个点中距离与自己较近的一个点的距离的最大值(所有点的结果取最大的值,即最远距离)最小. 意思应该都能明白. 解法:考虑将这棵树摆直如下: 那么我们可以把 ...
- POJ 1985 求树的直径 两边搜OR DP
Cow Marathon Description After hearing about the epidemic of obesity in the USA, Farmer John wants h ...
- luogu P3761 [TJOI2017]城市 树的直径 bfs
LINK:城市 谢邀,学弟说的一道毒瘤题. 没有真正的省选题目毒瘤 或者说 写O(n)的做法确实毒瘤. 这里给一个花20min就写完的非常好写的暴力. 容易想到枚举哪条边删掉 删掉之后考虑在哪两个点上 ...
- POJ 1849 Two(树的直径--树形DP)(好题)
大致题意:在某个点派出两个点去遍历全部的边,花费为边的权值,求最少的花费 思路:这题关键好在这个模型和最长路模型之间的转换.能够转换得到,全部边遍历了两遍的总花费减去最长路的花费就是本题的答案,要思考 ...
- POJ1985 树的直径(BFS
Cow Marathon Description After hearing about the epidemic of obesity in the USA, Farmer John wants ...
随机推荐
- 【NOIP2016提高A组8.12】总结
惨败!!!! 第一题是一道神奇的期望问题. 第二题,发现"如果两个部门可以直接或间接地相互传递消息(即能按照上述方法将信息由X传递到Y,同时能由Y传递到X),我们就可以忽略它们之间的花费&q ...
- git的初步研究1
工作中很多项目再往git上迁移,所以打算研究下git git是个版本控制系统 理解git工作区.暂存区.版本库的概念 工作区:在电脑中能看到的目录 暂存区:index即索引 即首先add加入暂存区 c ...
- vue项目图片路径问题
一般情况下我们为了能在本地显示效果,写图片路径会直接这样写,但是在实际中图片一般都是动态上传的, 所以,在vue中一般是这样的: 但是这样你会发现,图片根本显示不出来,只是显示了个图片的图标. 后来发 ...
- Vuex-全局状态管理【简单小案例】
前言: Vuex个人见解: 1.state :所有组件共享.共用的数据.理解为不是一个全局变量,不能直接访问以及操作它.2.mutations : 如何操作 state 呢?需要有一个能操作state ...
- jquery-时间轴滑动
效果预览图: html: <div class="tim"> <div class="timdiv"> <div class=&q ...
- 一天久坐办公室,怎么减fei?!
久坐的危害想必看到这篇文章的,一定都百度浏览了好多文章了,所以危害大家也都知道了,这里也就不一一列出. 久坐有危害,那么怎么减少危害呢,办法是什么???那就是不久坐啦.可是因为工作性质,不久坐臣妾恐怕 ...
- Python3 三元表达式、列表推导式、生成器表达式
Python3 三元表达式.列表推导式.生成器表达式 三元表达式 表达式中,有三个元素 name = input("请输入姓名: ")ret = '输入正确' if name == ...
- php array_unshift()函数 语法
php array_unshift()函数 语法 作用:用于向数组插入新元素.新数组的值将被插入到数组的开头.富瑞华 语法:array_unshift(array,value1,value2,valu ...
- ASCII 、UTF-8、Unicode编码
1.各种编码的由来 1.1.计算机编码的由来 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.所以只能是用一些数字来表示文本,这就是编码的由来.最早的计算机在设计时采用8个比 ...
- [CSP-S模拟测试]:幻魔皇(数学)
题目描述 幻魔皇拉比艾尔很喜欢斐波那契树,他想找到神奇的节点对. 所谓斐波那契树,根是一个白色节点,每个白色节点都有一个黑色节点儿子,而每个黑色节点则有一个白色和一个黑色节点儿子.神奇的节点对则是指白 ...