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. 关于SO_REUSEADDR的使用说明~

    参考WINDOWS   网络编程技术         1.   可以对一个端口进行多次绑定,一般这个是不支持使用的:     2.   对于监听套接字,比较特殊.如果你定义了SO_REUSEADDR, ...

  2. 【Redis】redis开机自启动、设置守护进程、密码设置、访问权限控制等安全设置(redis默认端口6379)

    一.redis设置开机自启动:centOS: 1.修改redis.conf中daemonize为yes,确保守护进程开启,也就是在后台可以运行. (守护进程:孤儿进程:独立于终端而存在的进程,不会因为 ...

  3. Android开发优化之——对界面UI的优化(1)

    在Android应用开发过程中,屏幕上控件的布局代码和程序的逻辑代码通常是分开 的.界面的布局代码是放在一个独立的xml文件中的,这个文件里面是树型组织的,控制着页面的布局.通常,在这个页面中会用到很 ...

  4. 初始化列表(const和引用成员)、拷贝构造函数

    一.构造函数初始化列表 推荐在构造函数初始化列表中进行初始化 构造函数的执行分为两个阶段 初始化段 普通计算段 (一).对象成员及其初始化  C++ Code  1 2 3 4 5 6 7 8 9 1 ...

  5. 迅为4412开发板Linux驱动教程——总线_设备_驱动注冊流程具体解释

    视频下载地址: 驱动注冊:http://pan.baidu.com/s/1i34HcDB 设备注冊:http://pan.baidu.com/s/1kTlGkcR 总线_设备_驱动注冊流程具体解释 • ...

  6. 一名全栈工程师Node.js之路-转

    Node.js 全球现状 虽然 Node.js 在国内没有盛行,但据 StackOverflow 2016 年开发者调查,其中 node.js .全栈.JavaScript 相关的技术在多个领域(包括 ...

  7. makefile之include

    "include"指示符告诉 make 暂停读取当前的 Makefile,而转去读取"include"指定的一个或者多个文件,完成以后再继续当前 Makefil ...

  8. 再谈Nginx Rewrite, 中文URL和其它

    上次谈到过Nginx和中文URL的问题,这几天又加深了认识. 多分享几个关于Nginx Rewrite的经验. Nginx匹配指定中文URL的方法:rewrite "(*UTF8)^x{66 ...

  9. ISTQB测试人员认证 初级(基础级)大纲

    ISTQB测试人员认证 初级(基础级)大纲 ---中文修订版本1(2015年5月6日) 2015-06-22 大纲pdf下载  ISTQB资料中心 在课程大纲中,每个章节都会提供相应的认知水平要求: ...

  10. cocosbuilder的一些坑

    主要是大小写问题 在扁平发布模式下,如果存在大小写不同的文件,文件会被替换掉.而模拟上运行没问题,在真机上运行 有问题.找了半天才发现,坑啊!