POJ 1383 Labyrinth (bfs 树的直径)
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 树的直径)的更多相关文章
- POJ 1383 Labyrinth (树的直径求两点间最大距离)
		
Description The northern part of the Pyramid contains a very large and complicated labyrinth. The la ...
 - POJ 1383题解(树的直径)(BFS)
		
题面 Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4997 Accepted: 1861 Descript ...
 - poj 1383 Labyrinth【迷宫bfs+树的直径】
		
Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4004 Accepted: 1504 Descrip ...
 - poj 1383 Labyrinth
		
题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...
 - poj 1985 Cow Marathon 树的直径
		
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
 - codeforce 337D Book of Evil ----树形DP&bfs&树的直径
		
比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...
 - BZOJ 3363 POJ 1985 Cow Marathon 树的直径
		
题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include < ...
 - codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)
		
题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...
 - POJ 3162 Walking Race(树的直径+单调队列)
		
题目大意:对一棵树,求出从每个结点出发能到走的最长距离(每个结点最多只能经过一次),将这些距离按排成一个数组得到dis[1],dis[2],dis[3]……dis[n] ,在数列的dis中求一个最长的 ...
 
随机推荐
- django 的多对多关系
			
django里自带的多对多表创建 其实就是两个多对一关系各自关联,在第三张表上 多对多的增加 add()可以传数值 例如 add(1)或数组 add(*[2,3]) 多对多反向操作 自己创建第三张表, ...
 - C语言第八周编程作业
			
这个作业属于哪个课程 C语言程序设计 这个作业要求在哪 https://edu.cnblogs.com/campus/zswxy/computer-scienceclass3-2018/hom ...
 - [Git] 020 stash —— Git 中的”皮姆粒子“
			
0. 我准备模拟两种情况 第一种 第二种 情况简析 仓库中的最新版本发现 Bug,需要立即修复 当前在 "dev" 分支中工作到一定程度,尚不能提交,但删之可惜 ps: 在分支中没 ...
 - urllib库认证,代理,cookie
			
认证,代理,cookie 1from urllib.request import HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm, buil ...
 - OracleOraDb11g_home1TNSListener服务无法启动
			
作者:伪墨 来源:CSDN 原文:https://blog.csdn.net/qq997404392/article/details/73296429 今天遇到OracleOraDb11g_home1 ...
 - ftok用法
			
转载: http://www.cnblogs.com/hjslovewcl/archive/2011/03/03/2314344.html http://www.cnblogs.com/lihaozy ...
 - 什么是lambda函数?有什么好处?
			
lambda 函数是一个可以接收任意多个参数(包括可选参数)并且返回单个表达式值的匿名函数 好处:1.lambda 函数比较轻便,即用即删除,很适合需要完成一项功能,但是此功能只在此一处使用,连名字都 ...
 - Python中函数传递参数有四种形式
			
Python中函数传递参数有四种形式 fun1(a,b,c) fun2(a=1,b=2,c=3) fun3(*args) fun4(**kargs) 四种中最常见是前两种,基本上一般点的教程都会涉及, ...
 - django编辑框实现
			
一些常用的: CKEditor UEEditor TinyEditor KindEditor 下载: http://kindeditor.net/down.php 使用方法: <textarea ...
 - 生成EXCEL文件是经常需要用到的功能,我们利用一些开源库可以很容易实现这个功能。
			
方法一:利用excellibrary,http://code.google.com/p/excellibrary/ excellibrary是国人写的开源组件,很容易使用,可惜貌似还不支持.xlsx( ...