POJ 1383 Labyrinth (树的直径求两点间最大距离)
Description
Input
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
Sample Input
2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######
Sample Output
Maximum rope length is 0.
Maximum rope length is 8.
Hint
If you use recursion, maybe stack overflow. and now C++/c 's stack size is larger than G++/gcc
#include<cstdio>
#include<queue>
#include<string.h>
#define M 1010
using namespace std;
int n,m,i,ans,flag[M][M],j,bx,by,kx,ky;
char str[M][M];
int dx[]={-,,,};
int dy[]={,,-,};
struct stu
{
int x,y,step;
}st;
void bfs(int xx,int yy)
{
memset(flag,,sizeof(flag));
stu next;
st.x=xx;
st.y=yy;
st.step=;
queue<stu>que;
flag[xx][yy] = ;
que.push(st);
while(!que.empty())
{
st=que.front();
que.pop();
for(int i = ; i < ; i++)
{
next.x=st.x+dx[i];
next.y=st.y+dy[i];
next.step=st.step+;
if(str[next.x][next.y] !='#' && next.x>= && next.y>= && next.x<m&&next.y<n&& flag[next.x][next.y] == )
{
flag[next.x][next.y]=;
if(ans < next.step)
{
ans=next.step;
kx=next.x;
ky=next.y;
}
que.push(next);
}
}
}
}
int main()
{
int t,k;
scanf("%d",&t);
while(t--)
{
ans=; k=;
scanf("%d %d",&n,&m);
for(i = ; i < m ; i++)
{
scanf("%s",str[i]);
if(k) continue;
for(j = ; j < n ; j++)
{
if(str[i][j] == '.')
{
bx=i;
by=j;
k=;
}
}
}
bfs(bx,by); //搜索出离bx,by距离最远的点kx,ky
bfs(kx,ky); //搜索出两点之间最大的距离
printf("Maximum rope length is %d.\n",ans);
}
}
POJ 1383 Labyrinth (树的直径求两点间最大距离)的更多相关文章
- poj 1383 Labyrinth【迷宫bfs+树的直径】
Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4004 Accepted: 1504 Descrip ...
- POJ 1383 Labyrinth (bfs 树的直径)
Labyrinth 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/E Description The northern part ...
- poj 1383 Labyrinth
题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- poj 2229 Ultra-QuickSort(树状数组求逆序数)
题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...
- POJ 3067 Japan (树状数组求逆序对)
POJ - 3067 题意:有(1-n)个城市自上到下在左边, 另有(1-m)个城市自上到下在右边,共有m条高速公路,现求这m条直线的交点个数,交点不包括在城市处相交. 题解:先将高速公路读入,然后按 ...
- hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- POJ 3067 Japan 树状数组求逆序对
题目大意:有两排城市,这两排城市之间有一些路相互连接着,求有多少条路相互交叉. 思路:把全部的路先依照x值从小到大排序,x值同样的依照y值从小到大排序,然后插入边的时候,先找有多少比自己y值小的,这些 ...
- POJ 1849 Two(树的直径--树形DP)(好题)
大致题意:在某个点派出两个点去遍历全部的边,花费为边的权值,求最少的花费 思路:这题关键好在这个模型和最长路模型之间的转换.能够转换得到,全部边遍历了两遍的总花费减去最长路的花费就是本题的答案,要思考 ...
随机推荐
- Centos 6.x 搭建 Zabbix Server
zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让 ...
- django_models表设计
和很多现代的web框架一样,django依赖于强大的数据访问层,试图将python面向对象特性和关系型数据库联系起来. 可移植性:不同的数据库,可以使用同一段代码,不用关心后台是哪家的数据库. 在一个 ...
- Random Query CodeForces - 846F
题目 翻译: 给出一个n个数字的数列a[1],...,a[n],f(l,r)表示使a[l],a[l+1],...,a[r]组成的新序列中的重复元素只保留一个后,剩下元素的数量(如果l>r,则在计 ...
- AO-XXXX
一 AO4419:应用于开关应用或PWM应用的场效应管.
- WPF日常需要使用的操作
窗体如何居中弹出 在窗体上添加属性 WindowStartupLocation="CenterScreen" 窗体如何隐藏掉windows边框 添加属性WindowStyle=& ...
- unity内存管理
最近一直在研究unity的内存加载,因为它是游戏运行的重中之重,如果不深入理解和合理运用,很可能导致项目因内存太大而崩溃. 详细说一下细节概念:AssetBundle运行时加载:来自文件就用Creat ...
- ES6学习笔记(12)----Reflect
参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ Reflect 1.概述:Object对象的内部方法都能在Reflect中找到,同时Reflec ...
- 严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:解决
严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal Contain ...
- bt设置指定的ip地址
auto eth0 iface eth0 inet staticaddress 192.168.1.112 IP地址netmask 255.255.255.0 子网掩码network 192.168. ...
- IOS代码收集
http://mobile.51cto.com/hot-410417.htm 退回输入键盘: - (BOOL) textFieldShouldReturn:(id)textField{ [textFi ...