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. 我的项目7 js 实现歌词同步(额,小小的效果)

    在项目中须要做一个播放器,还要实现歌词同步的效果,就跟如今搜狗音乐的歌词同步差点儿相同,在网上查了一些关于这方面的.整理了一下,在这里,其有用这种方法能够吗? <!DOCTYPE html> ...

  2. Linux-软件包管理-yum在线管理-网络yum源

    cd /etc/yum.repos.d/  切换到etc目录下面的yum.repos.d这个目录中ls   查看当前linux系统的yum源文件信息,其中CentOS-Base.repo文件为默认的y ...

  3. C++中四种类型转换以及const_cast是否能改变常量的问题

    we have four specific casting operators:dynamic_cast, reinterpret_cast, static_cast and const_cast. ...

  4. SQL server 2008定期的备份数据库及删除job

    在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以我们不可能要求管理员 每天守到晚上1点去备份数据库.要实现数据库的 ...

  5. js身份证验证算法

    var validateIdCard=function (id, backInfo) { var info={ y: "1900", m: "01", d: & ...

  6. django Multi-table inheritance ---- 用于实现基表-子表

    SQL中的父子表.在django中可以直接通过模式的继承来完成! 一.django中的model定义如下: 1.django定义 from django.db import models # Crea ...

  7. npm stripts 使用指南

    Node 开发离不开 npm,而脚本功能是 npm 最强大.最常用的功能之一. 本文介绍如何使用 npm 脚本(npm scripts). 一.什么是 npm 脚本? npm 允许在package.j ...

  8. FTPHelper-封装FTP的相关操作

    using System; using System.IO; using System.Net; using System.Text; namespace Whir.Software.DataSync ...

  9. 翻翻git之---给传统的搜索增添友好动画 JJSearchViewAnim

    转载请注明出处:王亟亟的大牛之路 这篇我又是个酱油,只是传播好东西也是一份功德. 由于节前被告知节后要把之前的EC项目翻成AS.那肯定要做一些加入新功能和旧实现替代的事,所以这两天也比較忙,正好另一些 ...

  10. SCUT入门-协议生成器配置

    协议生成器需要放在IIS里才能正常使用.具体目录在:Scut\Source\Tools\ContractTools\release 关于具体细节看这篇:https://github.com/ScutG ...