hdu 2612:Find a way(经典BFS广搜题)
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3226 Accepted Submission(s): 1045
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Y.#@
....
.#..
@..M Y.#@
....
.#..
@#.M Y..@.
.#...
.#...
@..M.
#...#
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
char a[][];
int step[][];
int m1[][];
int m2[][];
bool isw[][];
int dx[] = {,,,-};
int dy[] = {,,-,};
int n,m;
int ycurx,ycury;
int mcurx,mcury;
struct NODE{
int x;
int y;
};
int Min(int a,int b)
{
return a<b?a:b;
}
bool judge(int x,int y)
{
if( x< || y< || x>n || y>m ) //越界
return ;
if( isw[x][y] ) //走过了
return ;
if( a[x][y]=='#' ) //碰到墙
return ;
return ;
}
void bfs(int curx,int cury)
{
queue <NODE> q; //创建队列
NODE cur,next;
cur.x = curx;
cur.y = cury;
q.push(cur); //入队
memset(isw,,sizeof(isw));
isw[curx][cury] = true;
step[curx][cury] = ;
while(!q.empty()){
cur = q.front();
q.pop();
for(int i=;i<;i++){
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if( judge(nx,ny) )
continue;
step[nx][ny] = step[cur.x][cur.y] + ; //记录走到下一步的步数
isw[nx][ny] = true;
next.x = nx;
next.y = ny;
q.push(next);
}
}
}
int main()
{
while(cin>>n>>m){
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
cin>>a[i][j];
if(a[i][j]=='Y'){ //记录Y的位置
ycurx=i;
ycury=j;
}
else if(a[i][j]=='M'){ //记录M的位置
mcurx=i;
mcury=j;
}
}
int tmin = ;
bfs(ycurx,ycury); //以(ycurx,ycury)为起点开始广度优先搜索
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
m1[i][j]=step[i][j];
}
bfs(mcurx,mcury); //以(mcurx,mcury)为起点开始广搜优先搜索
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
m2[i][j]=step[i][j];
}
for(int i=;i<=n;i++) //遍历出最短的总步数
for(int j=;j<=m;j++){
step[i][j]=m1[i][j] + m2[i][j];
if(a[i][j]=='@'){
tmin = Min(tmin,step[i][j]);
}
}
cout<<tmin*<<endl; //输出最短总时间
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 2612:Find a way(经典BFS广搜题)的更多相关文章
- hdu 1180:诡异的楼梯(BFS广搜)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- hdu 1195:Open the Lock(暴力BFS广搜)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...
- hdu 1253:胜利大逃亡(基础广搜BFS)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- BFS广搜题目(转载)
BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...
随机推荐
- android基础-Apk打包过程(了解)
此文来源于<Android软件安全与逆向分析> 一.打包资料文件,生成R.java文件. 二.处理aidl文件,生成相应的Java文件. 三.编译工程源代码,生成相应的class文件. 四 ...
- Win8多平台引用配置
之前移植过DLNA的库,这个库是C++写的,然后我们的项目是C#的.接着很郁闷的事情发生了,主项目引用一个C#的DLL,然后这个DLL引用这个C++/CX封装的库.如果有C++的源代码的话,做项目依赖 ...
- 解决tomcat启动慢
1.在Tomcat环境中解决 可以通过配置JRE使用非阻塞的Entropy Source. 在catalina.sh中加入这么一行:-Djava.security.egd=file:/dev/./ur ...
- C#指南,重温基础,展望远方!(7)C#结构
结构是可以包含数据成员和函数成员的数据结构,这一点与类一样:与类不同的是,结构是值类型,无需进行堆分配. 结构类型的变量直接存储结构数据,而类类型的变量存储对动态分配的对象的引用. 结构类型不支持用户 ...
- Python装饰器(Decorator)简介
Python有许多出色的语言特性,装饰器(Decorator)便是其中一朵奇葩.先来看看一段代码: def deco1(f): print 'decorate 1' return f def deco ...
- Provisional headers are shown(一)
谷歌浏览器调试的时候,这个警告经常出现.但是每次产生的原因可能都是不一样的. 这篇文档记录我遇到的其中一次. 现象:一个并发的错误信息: CAUTION:request is not finished ...
- TypeError: can't convert console.log(...) to primitive type
一.背景 火狐浏览器提示这个错误,谷歌没有. 二.出错代码 var eventHandlers = { 'succeeded': function(e){ console.log('send succ ...
- 批量修改图像的大小 Python PIL
#-*-coding:utf-8-*- import os import os.path from PIL import Image import time def ResizeImage(filei ...
- Qt 检验器 三种典型类的用法
Qt提供了三个内置验证器类: QDoubleValidator, QIntValidator, QRegExpValidator. QDoubleValidator类: 提供了对浮点数的范围检查. Q ...
- [POJ 1236][IOI 1996]Network of Schools
Description A number of schools are connected to a computer network. Agreements have been developed ...