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)(好题)
大致题意:在某个点派出两个点去遍历全部的边,花费为边的权值,求最少的花费 思路:这题关键好在这个模型和最长路模型之间的转换.能够转换得到,全部边遍历了两遍的总花费减去最长路的花费就是本题的答案,要思考 ...
随机推荐
- Hexo瞎折腾系列(3) - 添加GitHub彩带和GitHub Corner
页面右上角添加GitHub彩带 你可以在这里找到一共12种样式的GitHub彩带,复制其中的超链代码. 在themes\next\layout\_layout.swig目录下找到头部彩带相关的代码: ...
- 简单实现人工智能:百度aip+tuling123
目录结构: app.py # -*- coding: utf-8 -*- # __author: ward # data: 2018/12/21 # @File: app from flask imp ...
- 使用ansible对远程主机上的ssh公钥进行批量分发
使用ansible对远程主机上的ssh公钥进行批量分发或者是删除修改操作 ansible内置了一个authorized_key模块,这个模块很好用,我们使用这个模块可以对远程 主机上的ssh公钥进行批 ...
- SPFA/Dijkstra POJ 3013 Big Christmas Tree
题目传送门 题意:找一棵树使得造价最少,造价为每个点的子节点造价和*边的造价和 分析:最短路跑出1根节点到每个点的最短边权值,然后每个点的权值*最短边距和就是答案,注意INF开足够大,n<=1特 ...
- 数组Reduce的应用
数组Reduce的应用 参考 简单应用 var arr = [1,2,3,4,5] var sum = arr.reduce(function (prev, cur, index, arr) { co ...
- Vue不兼容IE8原因以及Object.defineProperty详解
Vue不兼容IE8原因以及Object.defineProperty详解 原因概述: Vue.js使用了IE8不能模拟的ECMAScript5特性. Vue.js支持所有兼容ES5的浏览器. Vue将 ...
- Netbeans自定义折叠代码
只需要在模块开始注释以//<editor-fold>开始, 在模块结束行以 //</editor-fold>结束即可 Can I Create Custom Code Fold ...
- MSComDlg.CommonDialog服务器不能创建对象错误的解决
作者:朱金灿 来源:http://blog.csdn.net/clever101 在JavaScript中弹出打开文件对话框,代码如下: var fileOpenDlg = new ActiveXOb ...
- mac下安装nodejs
下载 https://nodejs.org/en/ 安装 一步步继续就ok 验证 npm -v node -v Done!
- win应用只允许单个实例运行,并将已运行实例窗口置顶
关键词:windows,c++,桌面应用,单个实例,窗口置顶 目标:1.判断本程序是否已有一个实例在运行.2.若有,则激活已在运行的实例(将其窗口置顶),并退出当前运行. 1.使用semaphore来 ...