poj 1383 Labyrinth【迷宫bfs+树的直径】
| Time Limit: 2000MS | Memory Limit: 32768K | |
| Total Submissions: 4004 | Accepted: 1504 |
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. 题意:一段绳子,可以在迷宫内使用,要求这段绳子可以从迷宫内任意一点到任意的另一点,求绳子的最小长度
题解:求迷宫内最长路径的长度,利用树的直径的求法,进行两次bfs
利用队列记距离:
#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 1010
using namespace std;
char map[MAX][MAX];
bool vis[MAX][MAX];
int n,m;
int ans,sum,x1,y1,x2,y2;
struct node
{
int x,y;
int ong;
};
void getmap()
{
int i,j;
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(map[i][j]=='.')
{
x1=i;
y1=j;
return ;
}
}
}
bool judge(int c,int r)
{
if(c>=0&&c<n&&r>=0&&r<m&&map[c][r]!='#'&&!vis[c][r])
return true;
return false;
}
void bfs(int x1,int y1)
{
int i,j;
int move[4][2]={0,1,0,-1,1,0,-1,0};
memset(vis,false,sizeof(vis));
node beg,end;
queue<node>q;
beg.x=x1;
beg.y=y1;
beg.ong=0;
vis[x1][y1]=true;
q.push(beg);
while(!q.empty())
{
end=q.front();
q.pop();
for(i=0;i<4;i++)
{
beg.x=end.x+move[i][0];
beg.y=end.y+move[i][1];
if(judge(beg.x,beg.y))
{
vis[beg.x][beg.y]=true;
beg.ong=end.ong+1;
if(ans<beg.ong)
{
ans=beg.ong;
x2=beg.x;
y2=beg.y;
}
q.push(beg);
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
getchar();
scanf("%d%d",&m,&n);
ans=0;
getmap();
bfs(x1,y1);
bfs(x2,y2);
printf("Maximum rope length is %d.\n",ans);
}
return 0;
}
利用dis[]数组记录距离:
#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 1010
using namespace std;
char map[MAX][MAX];
bool vis[MAX][MAX];
int dis[MAX][MAX];
int n,m;
int ans,sum,x1,y1,x2,y2;
struct node
{
int x,y;
};
void getmap()
{
int i,j;
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(map[i][j]=='.')
{
x1=i;
y1=j;
return ;
}
}
}
bool judge(int c,int r)
{
if(c>=0&&c<n&&r>=0&&r<m&&map[c][r]!='#'&&!vis[c][r])
return true;
return false;
}
void bfs(int x1,int y1)
{
int i,j;
int move[4][2]={0,1,0,-1,1,0,-1,0};
memset(vis,false,sizeof(vis));
memset(dis,0,sizeof(dis));
node beg,end;
queue<node>q;
beg.x=x1;
beg.y=y1;
vis[x1][y1]=true;
q.push(beg);
while(!q.empty())
{
end=q.front();
q.pop();
for(i=0;i<4;i++)
{
beg.x=end.x+move[i][0];
beg.y=end.y+move[i][1];
if(judge(beg.x,beg.y))
{
vis[beg.x][beg.y]=true;
dis[beg.x][beg.y]=dis[end.x][end.y]+1;
if(ans<dis[beg.x][beg.y])
{
ans=dis[beg.x][beg.y];
x2=beg.x;
y2=beg.y;
}
q.push(beg);
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
getchar();
scanf("%d%d",&m,&n);
ans=0;
getmap();
bfs(x1,y1);
bfs(x2,y2);
printf("Maximum rope length is %d.\n",ans);
}
return 0;
}
poj 1383 Labyrinth【迷宫bfs+树的直径】的更多相关文章
- 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(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- URAL 1145—— Rope in the Labyrinth——————【求树的直径】
Rope in the Labyrinth Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- codeforce 337D Book of Evil ----树形DP&bfs&树的直径
比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...
- 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 1985 Cow Marathon【树的直径】
题目大意:给你一棵树,要你求树的直径的长度 思路:随便找个点bfs出最长的点,那个点一定是一条直径的起点,再从那个点BFS出最长点即可 以下研究了半天才敢交,1.这题的输入格式遵照poj1984,其实 ...
- POJ 1383 Labyrinth (树的直径求两点间最大距离)
Description The northern part of the Pyramid contains a very large and complicated labyrinth. The la ...
随机推荐
- 吐槽:Lambda表达式
前面我曾经讨论过Lambda表达式(也就是匿名表达式)的用法, 这里我就主要强调一下匿名表达式的好处. 首先是不需要写多余的方法体,特别是订阅事件的时候,但是也有一个问题,那就是单个方法会因为匿名表达 ...
- 1 Winform 异步更新控件
刚才看到有人问为了winfrom中,在大数据绑定的时候出现画面假死的状态,为了解决这个问题希望通过再开一个线程来给控件绑定数据,可是画面还是会假死.现在看到的方法有1.掩耳盗铃法(Control.Ch ...
- 初尝 MVC4
文章内容参考 http://www.cnblogs.com/leoo2sk/archive/2008/10/27/1320285.html 开发环境 VS2010 ,VS2010 开发 MVC4 需下 ...
- boost 1.56.0 编译
编译步骤及参数说明: http://www.cnblogs.com/zhcncn/p/3950477.html 编译64位版本: http://www.cnblogs.com/codingmylife ...
- HDU1557权利选举
/* 思路:遍历所有2^n个集合,对于每个集合求票和,如果满足票为优胜团体,而再对集合每个成员比较,是否满足变成非优胜团体,是的话,对于该成员对应结果+1. 重点:利用二进制思想,所有团体均对应0~2 ...
- gpload的简单实用
准备工作: 1.因为gpload是对gpfdist的封装,因此使用gpload之前必须开启gpfdist的服务,不然无法使用. gpfdist -d /home/admin -p 8181 -l /t ...
- 配置处理结果result
Action处理完用户请求后返回一个字符串,整个字符串就是一个逻辑视图名. 除此之外,struts2还支持多种结果映射,struts2将结果转为实际资源时,不仅可以是JSP视图资源,也可以是FreeM ...
- 中国天气网接口返回json格式分析及接口(XML、图片接口)说明
实时天气: city "北京"//城市 cityid "101010100"//城市编码 temp "17&qu ...
- silverlight中DataGrid数据高亮显示
效果如图所示, <UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.W ...
- c++和java(c#)之间的pk
个人认为本文较偏激,且年代较久远,但可以一看. 转自c++和java(c#)之间的pk 1.谁好谁坏? 如同当初我没有想到会进入java阵营一样,这次闯入c++阵营也是意料之外的.多年前,受到微软的影 ...