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 ...
随机推荐
- 设置appicon和启动图
设置appicon General – App Icon Source 设置AppIcon 图片大小 iphone 6P(@3x) 180x180 iphone 6(@2x) iphone5/5s ( ...
- vs2013 使用vs调试器,发现调试器显示的数据错误。查看内存,发现内存是正确的。
有可能只是调试器的问题,程序可以正常运行的! 网上没找到此种情况解释.感觉有可能是那里堆被破坏了.
- Photon引擎开发实战(1)——Photon 简介
Photon简介 Photon是一套使用广泛的socket server引擎,服务端底层C++编写,客户端C#编写,跨多平台,收费,效率可观的一款引擎.实用上前有九城游戏(原魔兽世界代理),现在笔者发 ...
- QTableWidget中添加按钮
添加按钮 void QTableWidget::setCellWidget ( int row, int column, QWidget * widget ) widget可以是自己定义的按钮 cla ...
- 日期-用Datapicker实现前一天后一天
运用了JQuery UI Datepicker 插件和一些常用日期的方法.其中Datepicker的API具体可参考[http://api.jqueryui.com/datepicker/#optio ...
- 使用Node.js作为后台进行爬虫
看了一遍又一遍Node.js但是没过多久就又忘了,总想找点东西来练练手,就发现B站首页搜索框旁边的GIF图特别有意思,想着是不是可以写一个小Node.js项目把这些图全部扒下来,于是带着复习.预习与探 ...
- lamp环境中的/到底是指的网站根目录还是linux的根
在lamp中经常会用到 require,require_once等包含文件的语句. 如果你使用相对路径没有问题. 但是如果你使用了这样的语句就要小心了. 对于如图网站目录 require_once ' ...
- DELPHI TMS Advanced Charts 3.8.0.3 Full Source D6-XE6 控件分享
仅供大家学习使用,请大家支持正版!! TMS Advanced Charts 3.8.0.3 Full Source D6-XE6 该控件用来画图标,压缩包里还有FOR INTRAWEB的版本 链接: ...
- GHOST备份
整理日: 2015年2月16日 GHOST Disk2GHO STEP01 STEP02 STEP03 STEP04 STEP05 STEP06 STEP07 STEP08 STEP09 STEP10
- python ^M 产生的原因 及 lxml 如何获取text的原因
其实跟踪一下会发现,^M 是由于 /r/n 所造成的. article = dom_c.xpath('//td[@id="article_content"]//text()')co ...