题面

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)的更多相关文章

  1. poj2631 树的直径 + bfs

    //Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iost ...

  2. POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)

    树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...

  3. hdu2196 树的直径 + bfs

    //Accepted 740 KB 15 ms //树的直径 //距离一个顶点最远的点一定是树的直径的一个端点 #include <cstdio> #include <cstring ...

  4. 树上选两点(使最短)树的直径+bfs

    题意: 给你一颗树,让你放两个点,放在哪里的时候任意点到某个最近的消防站最远值最小. 思路: 树的直径类题目. 首先我们想两个点会把整棵树分成两个团,所以肯定会在树的某个链上切开. 而且要切一定切在树 ...

  5. ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS

    题意:给一棵树,要求找出两个点,使得所有点到这两个点中距离与自己较近的一个点的距离的最大值(所有点的结果取最大的值,即最远距离)最小. 意思应该都能明白. 解法:考虑将这棵树摆直如下: 那么我们可以把 ...

  6. POJ 1985 求树的直径 两边搜OR DP

    Cow Marathon Description After hearing about the epidemic of obesity in the USA, Farmer John wants h ...

  7. luogu P3761 [TJOI2017]城市 树的直径 bfs

    LINK:城市 谢邀,学弟说的一道毒瘤题. 没有真正的省选题目毒瘤 或者说 写O(n)的做法确实毒瘤. 这里给一个花20min就写完的非常好写的暴力. 容易想到枚举哪条边删掉 删掉之后考虑在哪两个点上 ...

  8. POJ 1849 Two(树的直径--树形DP)(好题)

    大致题意:在某个点派出两个点去遍历全部的边,花费为边的权值,求最少的花费 思路:这题关键好在这个模型和最长路模型之间的转换.能够转换得到,全部边遍历了两遍的总花费减去最长路的花费就是本题的答案,要思考 ...

  9. POJ1985 树的直径(BFS

    Cow Marathon   Description After hearing about the epidemic of obesity in the USA, Farmer John wants ...

随机推荐

  1. Behavior行为

    创建公用的js 模块 封装起来 let behavior = Behavior({//定义属性 properties: { type: String, img: String, content: St ...

  2. Springboot配置文件占位符

    一.配置文件占位符 1.application.properties server.port=8088 debug=false product.id=ID:${random.uuid} product ...

  3. lnamp完整版[linux+apache2.4+php5.6.6+mysql5.6]

    Lnamp环境安装实录 将采用的开源软件: Apache [WEB动态脚本服务器,做nginx的反向代理 8080端口] Tengine [WEB静态文件服务器 80端口] MySQL PHP .Ap ...

  4. java web中乱码的种类和一些解决方式

    在java web课堂测试中遇到了一些乱码问题 ,从百度上找到了许多种解决方法和乱码的种类,在这里总结一下. 一.文件出现乱码 [右击文件]->[Properties]->[Resourc ...

  5. MongoDB的特殊操作

    相比关系型数据库, Array [1,2,3,4,5] 和 Object { 'name':'DragonFire' } 是MongoDB 比较特殊的类型了 特殊在哪里呢?在他们的操作上又有什么需要注 ...

  6. php reset()函数 语法

    php reset()函数 语法 作用:将内部指针指向数组中的第一个元素,并输出.博智达 语法:reset(array) 参数: 参数 描述 array  必需.规定要使用的数组. 说明:若成功则返回 ...

  7. getCurrentPages

    解释:getCurrentPages 全局函数用于获取当前页面栈的实例,以数组形式按栈的顺序给出,第一个元素为首页,最后一个元素为当前页面. 示例: // index.jsPage({ onShow( ...

  8. ckeditor直接粘贴图片实现

    自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...

  9. php大文件上传解决方案

    PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...

  10. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...