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

Problem Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
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.
 
Input
The input contains multiple test cases.
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
 
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
 
Sample Input
Y.#@
....
.#..
@..M Y.#@
....
.#..
@#.M Y..@.
.#...
.#...
@..M.
#...#
Sample Output
66
88
66
 
Author
yifenfei
 
Source
 
Recommend
yifenfei   |   We have carefully selected several similar problems for you:  2717 1254 1728 2102 1072 

 
  bfs简单广搜题。入门级难度,但是因为第一次做,还是耗费了不少时间。
  题意是Y和M要去KFC一起吃饭,@为KFC的位置,可以有多个,求出他们到达KFC耗费的最短时间(1个格子耗费11分钟)。
  用两次bfs(),分别求出Y和M到各KFC的最短路,相加之后,遍历出最短的总时间。
  第一次练习广搜的题,代码写的有些费力:
 
 #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广搜题)的更多相关文章

  1. hdu 1180:诡异的楼梯(BFS广搜)

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Subm ...

  2. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. 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 ...

  4. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  5. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  6. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  8. hdu 1253:胜利大逃亡(基础广搜BFS)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. BFS广搜题目(转载)

    BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...

随机推荐

  1. 转:RHEL6.3 安装GCC 记录

    本文参考:http://blog.163.com/phys_atom/blog/static/1676445532012229814992/ 如果直接使用GUN GCC官方的源码来安装是不成功的,因为 ...

  2. 微信小程序—智能小蜜(基于智能语义解析olami开放平台)

    概述 该程序支持功能有查天气.查诗词.查百科.算算术.查日历.看笑话.看故事.聊天等,通过用户输入语句智能解析用户意图输出相应答案. 详细 代码下载:http://www.demodashi.com/ ...

  3. php 实现打印预览的功能

    <inputid="btnPrint" type="button" value="打印"onclick="javascrip ...

  4. git的常见操作方法

    GIT操作方法 http://git.oschina.net/ g进入https://git-for-windows.github.io/下载安装 g启动命令窗口输入以下内容 git config - ...

  5. 博客已迁移至512z.com

    本博客已迁移至http://blog.512z.com,此处今后不再更新

  6. Java类载入器(一)——类载入器层次与模型

    类载入器   虚拟机设计团队把类载入阶段中的"通过一个类的全限定名来获取描写叙述此类的二进制字节流"这个动作放到Java虚拟机外部去实现.以便让应用程序自己决定怎样去获取所须要的类 ...

  7. UVA - 1218 Perfect Service(树形dp)

    题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...

  8. MySQL Fabric部署

    架构描写叙述: 一台主机上安装4个MySQL 服务,当中一个MySQL服务用于存储MySQL Fabric后台数据:另外3个MySQL服务用于主从架构測试.一个主+两个从. 第一部分:二进制方式安装M ...

  9. cocos2d-x 2.0通过CCAnimation实例获取CCSpriteFrame

    通过CCAnimation实例获取CCSpriteFrame,会出现类型转换问题.我们在创建一个animation的时候,经常遵循下面的步骤:1)create 一个CCArray对象A.2)通过A-& ...

  10. 单调栈poj2796

    题意:给你一段区间,需要你求出(在这段区间之类的最小值*这段区间所有元素之和)的最大值...... 例如: 6 3 1 6 4 5 2 以4为最小值,向左右延伸,6 4 5  值为60....... ...