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. eclipse maven Cannot change version of project facet Dynamic web module to 3.0

      eclipse maven Cannot change version of project facet Dynamic web module to 3.0 (eclipse 修改maven项目的 ...

  2. js中ip地址与整数的相互转换

    转载地址 //IP转成整型function _ip2int(ip) {    var num = 0;    ip = ip.split(".");    num = Number ...

  3. 【Linux】centos和ubuntu下php5安装redis2.24扩展

    1.服务器先安装redis-server,这是毋庸置疑的!!! 2.服务器开启redis-server,配置相关参数 3.配置好redis服务器后,再安装php的redis扩展phpredis. 一. ...

  4. android多线程进度条

    多线程实现更新android进度条. 实例教程,详细信息我已经注释   android多线程进度条   01package com.shougao.hello; 02 03import android ...

  5. sqlserver学习笔记(一)—— 登录本机sqlserver、启动和停止sqlserver服务、创建和删除数据库

    (重要参考:51自学网——SQL Server数据库教程) 首先按照网上教程安装好sqlserver,打开登录 登录本机sqlserver:①. ②localhost ③127.0.0.1 启动和停止 ...

  6. 第23章、OnFocuChangeListener焦点事件(从零开始学Android)

      在Android App应用中,OnFocuChangeListener焦点事件是必不可少的,我们在上一章的基础上来学习一下如何实现. 基本知识点:OnFocuChangeListener事件 一 ...

  7. dbcp2连接池获取数据库连接Connection

    一.先来看看手工创建的方式 public static Connection getConnection() { Connection conn = null; try { Class.forName ...

  8. AutoFac文档3(转载)

    目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 服务类型,名称和键 同 ...

  9. [Informix] unload load

    select tabname from systables where tabname  like 'aa%' select * from syscolumns where tabname like ...

  10. COM方式实现C++调用C#代码的一些总结

    首先这个测试没成功,只在本机上可行,在不同机器上测试失败.可能是GUID不对或者没注册成功. 既然已经花了一上午时间去研究,还是总结一下 1.网上说要创建一个snk证书,但不创建也可以.只不过不能放入 ...