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 ...
随机推荐
- Flutter-常用插件庫
alibaba/flutter_boost:路由 install_plugin 2.0.0#app下载更新插件 audio_recorder: any #录音.播放 flutter_sound: ^1 ...
- MYSQL数据导出与导入,secure_file_priv参数设置
https://www.imooc.com/article/41883 MySQL 报错 [Code: 1290, SQL State: HY000] The MySQL server is run ...
- 模块打包 webpack
1.模块打包工具 2.工作方式: 1)将存在依赖关系的模块按照特定规则合并为单个JS文件,一次全部加载进页面中 2)在页面初始时加载一个入口模块,其他模块异步的进行加载 3.优势: 1)支持AMD,C ...
- IDEA将新建项目上传至GitLab
1.首先,需要你自己登录GitLab,并新建一个项目的链接,如下图所示: (此图为图三,该链接下面操作中将会用到!) 2.在idea上新建一个项目,完成之后,需要创建一个git仓库: 3.然后可以根据 ...
- Logstash介绍及Input插件介绍
一.Logstash简介 Logstash是一个开源数据收集引擎,具有实时管道功能.Logstash可以动态地将来自不同数据源的数据统一起来,并将数据标准化到你所选择的目的地. Logstash管 ...
- Collections 索引
About Me NOIp 数据结构专题总结 NOIp 图论算法专题总结 NOIp 基础数论知识点总结 NOIp 数学知识点总结 搜索算法总结 (不包含朴素 DFS, BFS) 位运算 字符串算法总结 ...
- mpvue 未找到app.json入口文件
[情况1] 通过 vue init mpvue/mpvue-quickstart myproject 创建一个mpvue项目,发现项目中没有app.json文件. 解决:npm run dev 编译一 ...
- 2.Vue子组件给父组件通信
子组件给父组件通信 如果子组件想要改变数据呢?这在vue中是不允许的,因为vue只允许单向数据传递,这时候我们可以通过触发事件来通知父组件改变数据,从而达到改变子组件数据的目的 子组件: <te ...
- ParaEngine 一个同事的公司的开源引擎
看说明作者是李西峙,浙大研究生,靠投资研发此引擎,10年了,大概翻了下github里的文件,值得收藏,至少里面有voxelmesh https://github.com/LiXizhi/NPLRunt ...
- mvn 与 pom.xml
mvn 安装 wget http://mirrors.hust.edu.cn/apache//maven/maven-3/3.0.5/binaries/apache-maven-3.0.5-bin.t ...