题面

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. java编写算法题格式(链表和二叉树)

    (1)链表 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; ...

  2. 封装插件并发布到npm的历程

    1.封装插件 2.使用webpack打包的时候发生了很多问题 3.过程很反复 4.但最终还是发布成功了

  3. python入门 python字符串换行显示、字符串太长\连接多行

    #coding:utf-8#/usr/bin/python"""2018-11-03dinghanhua缩进换行""" "&quo ...

  4. 向上取整&向下取整

    使用floor函数. floor(x)返回的是小于或等于x的最大整数.eg.      floor(1.5) = 1 floor(-2.5) = -3 使用ceil函数. ceil(x)返回的是大于x ...

  5. [洛谷3934]P3934 Nephren Ruq Insania题解

    先放个奈芙莲 解法 看到这种题目就知道是欧拉降幂,然后根据某玄学证明,递归欧拉降幂从l到r不会超过\(\Theta(log_n)\),所以直接递归解决,然后区间修改直接树状数组维护一下 然后就A了 代 ...

  6. c++复习——类(2)

    1.this指针 this指针是一个指向对象的指针.  this指针是一个隐含于成员函数中的对象指针.  this指针是一个指向正在调用成员函数的对象的指针.  类的静态成员函数没有this指针 ...

  7. 三、angular7登录请求和路由带参传递

    在 app.module.ts 中引入 HttpClientModule 并注入 import {HttpClientModule} from '@angular/common/http'; impo ...

  8. [CSP-S模拟测试]:购物(柯朵莉树)

    题目描述 $visit_world$有一个商店,商店里卖$N$个商品,第$i$个的价格为$a[i]$我们称一个正整数$K$是美妙的,当且仅当我们可以在商店里选购若干个商品,使得价格之和落在区间$[K, ...

  9. 走进JavaWeb技术世界14:Mybatis入门

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...

  10. 1px渲染成2px的场景及解决方案

    1.场景一: IE6 下默认div最小高度为2px,如何创建高为1px的容器? .minContainer{font-size:0px;overflow:hidden}   2.场景二: 移动端高分辨 ...