POJ 1383 Labyrinth (bfs 树的直径)

时间:2023-02-19 09:56:46

Labyrinth

题目链接:

http://acm.hust.edu.cn/vjudge/contest/130510#problem/E

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

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.
```

Source


2016-HUST-线下组队赛-5


##题意:

以邻接矩阵的形式给出树,求树的直径.


##题解:

两遍搜索求树的直径即可.
由于数据规模较大,这题很容易爆栈,所以得用bfs.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define maxn 1010
#define inf 0x3f3f3f3f
#define mod 1000000007
#define mid(a,b) ((a+b)>>1)
#define IN freopen(".in","r",stdin);
using namespace std;

int n,m;

char mp[maxn][maxn];

bool is_ok(int x, int y) {

return x>=0 && y>=0 && x<n && y<m;

}

int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

int ans,px,py;

bool vis[maxn][maxn];

//void dfs(int x, int y, int step) {

// vis[x][y] = 1;

// bool flag = 0;

// for(int i=0; i<4; i++) {

// int xx = x + dir[i][0];

// int yy = y + dir[i][1];

// if(!is_ok(xx,yy) || mp[xx][yy]=='#' || vis[xx][yy]) continue;

// dfs(xx, yy, step+1);

// flag = 1;

// }

//

// if(!flag && step > ans) {

// ans = step;

// px = x, py = y;

// }

//}

struct node {

int x,y,step;

};

void bfs() {

queue q; while(!q.empty()) q.pop();

node cur,next;

memset(vis, 0, sizeof(vis));

cur = {px,py,0}; vis[px][py] = 1;

q.push(cur);

while(!q.empty()) {

bool flag = 0;

cur = q.front(); q.pop();

for(int i=0; i<4; i++) {

int xx = cur.x + dir[i][0];

int yy = cur.y + dir[i][1];

if(!is_ok(xx,yy) || mp[xx][yy]=='#' ||vis[xx][yy]) continue;

vis[xx][yy] = 1;

next = {xx,yy,cur.step+1};

q.push(next);

flag = 1;

}

if(!flag && cur.step > ans) {

ans = cur.step;

px = cur.x; py = cur.y;

}

}

}

int main()

{

IN;

int T;
cin >> T; while (T--){ scanf("%d %d", &m, &n); getchar();
for (int i = 0; i < n; i++) {
gets(mp[i]);
} px = py = -1;
for (int i = 0; i < n; i++) {
for(int j=0; j < m; j++) {
if(mp[i][j] == '.') {
px = i, py = j;
break;
}
}
if(px != -1) break;
} ans = 0;
bfs();
ans = 0;
bfs(); printf("Maximum rope length is %d.\n", ans);
} return 0;

}

POJ 1383 Labyrinth (bfs 树的直径)的更多相关文章

  1. POJ 1383 Labyrinth (树的直径求两点间最大距离)

    Description The northern part of the Pyramid contains a very large and complicated labyrinth. The la ...

  2. POJ 1383题解(树的直径)(BFS)

    题面 Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4997 Accepted: 1861 Descript ...

  3. poj 1383 Labyrinth【迷宫bfs&plus;树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

  4. poj 1383 Labyrinth

    题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...

  5. poj 1985 Cow Marathon 树的直径

    题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...

  6. codeforce 337D Book of Evil ----树形DP&amp&semi;bfs&amp&semi;树的直径

    比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...

  7. BZOJ 3363 POJ 1985 Cow Marathon 树的直径

    题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include < ...

  8. codeforces 690C2 C2&period; Brain Network &lpar;medium&rpar;&lpar;bfs&plus;树的直径&rpar;

    题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...

  9. POJ 3162 Walking Race&lpar;树的直径&plus;单调队列&rpar;

    题目大意:对一棵树,求出从每个结点出发能到走的最长距离(每个结点最多只能经过一次),将这些距离按排成一个数组得到dis[1],dis[2],dis[3]……dis[n] ,在数列的dis中求一个最长的 ...

随机推荐

  1. BPM与 SAP &amp&semi; Oracle EBS集成解决方案分享

    一.需求分析 SAP和Oracle EBS都是作为全球*的的ERP产 品,得到了众多客户的青睐.然而由于系统庞大.价格昂贵以及定位不同,客户在实施过程中经常会面临以下困惑: 1.SAP如何实现&qu ...

  2. spring boot&lpar;六&rpar;:如何优雅的使用mybatis

    *:first-child{margin-top: 0 !important}.markdown-body>*:last-child{margin-bottom: 0 !important}.m ...

  3. JavaWeb chapter10 JavaWeb开发模式

    1.  开发模式 (1)开发模式1:JSP+JavaBean (2)开发模式2:Servlet+JSP+JavaBean (MVC) 2.JavaBean 本质上是一个普通的Java类:需要遵循一定的 ...

  4. Python面试必须要看的15个问题

    本文由EarlGrey@编程派独家编译,转载请务必注明作者及出处. 原文:Sheena@codementor 译文:编程派 引言 想找一份Python开发工作吗?那你很可能得证明自己知道如何使用Pyt ...

  5. 基于Python的密码生成程序的优化

    近期刚刚组织完内部的Python基础培训.GUI的开发培训,之后布置的作业是两人一组,利用前面所写的一些模块做一些小软件. 具体就是模拟Advanced Password Generator这个软件的 ...

  6. Fatal error&colon; Class &OpenCurlyQuote;mysqli’ not found in解决办法

    在使用[$conn = new \mysqli($servername, $username, $password,$dbname);]连接msql数据库的时候 出现错误:[Fatal error: ...

  7. 关于css盒模型

    在css中,width和height指的是内容区域的宽度和高度.增加内边距,边框和外边距不会影响内容区域的尺寸,但是会增加元素框的总尺寸.假设框的每个边上有10个像素的外边距和5像素的内边距,如果希望 ...

  8. C&plus;&plus;中虚拟继承

    多重继承 在多重继承中,基类的构造函数的调用次序即不受派生类构造函数初始化列表中出现的基类构造函数的影响,也不受基类在构造函数初始化列表中的出现次序的影响,它按照基类在类派生列表中的出现次序依次调用相 ...

  9. 戏说程序猿之cannot find the object

    “别开玩笑了,程序员哪里需要对象!” 程序员难找对象原因无非如下: 1.工作时间长,恋爱时间少 2.性格偏于内向,不主动 3.不注意个人形象 程序员爱情观: 爱情就是死循环,一旦执行就陷进去了: 爱上 ...

  10. Java学习前的一些准备

    1.JDK - (Java SE Development Kit) JDK是Java开发所需要的环境,就跟我们想玩某个网游一样,玩之前一定是需要先安装相应的程序包的.那这个JDK就是我们准备登陆Jav ...