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

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

Sample Output

66
88
66
解题思路:问两者到达同一家KFC所花费的最小时间,典型的bfs解法,不过这里要两次bfs,同时用两个二维数组分别记录从'Y'、'M'到达整个图中每个坐标点所花费的最少步数,最后遍历一下图中每个KFC--'@'位置上得到的最小步数之和即可。
AC代码:
 #include<iostream>
#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
const int maxn=;
int n,m,fx,fy,mx,my,mp1[maxn][maxn],mp2[maxn][maxn],dir[][]={{,-},{,},{-,},{,}};
char mp[maxn][maxn];
bool vis[maxn][maxn];
struct node{int x,y;}nod,tmp;//记录到当前坐标的最短距离
queue<node> que;
void bfs(int x,int y,int dis[][maxn]){//dis数组记录从出发点到达每个坐标位置的最短距离
while(!que.empty())que.pop();//清空
memset(vis,false,sizeof(vis));//标记为全部未访问状态
nod.x=x,nod.y=y;
vis[x][y]=true;
que.push(nod);
while(!que.empty()){
nod=que.front();que.pop();
for(int i=;i<;++i){
int nx=nod.x+dir[i][],ny=nod.y+dir[i][];
if(nx>=&&ny>=&&nx<n&&ny<m&&mp[nx][ny]!='#'&&!vis[nx][ny]){
dis[nx][ny]=dis[nod.x][nod.y]+;//到达邻接点的步数为到达上一个点的步数加1
vis[nx][ny]=true;
tmp.x=nx,tmp.y=ny;//要用临时变量来存储,不然会出错
que.push(tmp);
}
}
}
}
int main(){
while(cin>>n>>m){
for(int i=;i<n;++i){
for(int j=;j<m;++j){
cin>>mp[i][j];
if(mp[i][j]=='Y'){fx=i;fy=j;}//标记Y、M的坐标位置
if(mp[i][j]=='M'){mx=i;my=j;}
}
}
memset(mp1,,sizeof(mp1));//全部初始化为0
memset(mp2,,sizeof(mp2));
bfs(fx,fy,mp1);
bfs(mx,my,mp2);
int dist=;
for(int i=;i<n;++i)
for(int j=;j<m;++j)
if(mp[i][j]=='@'&&mp1[i][j]!=&&mp2[i][j]!=)
//如果是KFC即mp[i][j]=='@'并且mp1[i][j]和mp2[i][j]都不等于0,因为可能会达不到,所以两者都不能为0
dist=min(mp1[i][j]+mp2[i][j],dist);
cout<<dist*<<endl;
}
return ;
}

题解报告:hdu 2612 Find a way(双bfs)的更多相关文章

  1. HDU.2612 Find a way (BFS)

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

  2. HDU - 2612 Find a way 双起点bfs(路径可重叠:两个队列分别跑)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU - 2612 Find a way 【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意 有两个人 要去一个城市中的KFC 一个城市中有多个KFC 求两个人到哪一个KFC的总时间最 ...

  4. hdu 2612 Find a way(BFS)

    题目链接:hdu2612 思路:题意是求两个人到某一个KFC花费时间和最小,其实就是求最短距离和,用两个BFS,分别以两个人为起点,分别记录下两人到每个KFC的距离,然后求出最小的和 #include ...

  5. (简单) HDU 2612 Find a way,BFS。

    Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...

  6. HDU - 2612 Find a way(BFS搜索)

    题目: 链接 思路: 用BFS分别以‘Y’和‘M’的位置为起点进行两次搜索,并把这两次的搜索结果在一个二维数组中保存下来,在对地图遍历遇到‘@’更行最小值. PS: 如果用‘Y’和‘M’点分别去搜每个 ...

  7. 题解报告:poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  8. 题解报告:hdu 1398 Square Coins(母函数或dp)

    Problem Description People in Silverland use square coins. Not only they have square shapes but also ...

  9. 题解报告:hdu 2069 Coin Change(暴力orDP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069 Problem Description Suppose there are 5 types of ...

  10. 题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)

    Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...

随机推荐

  1. 国内程序员的十大疑问之一:为什么老外不愿意用MyBatis?

    老外用MyBatis吗 昨天我在我在知乎看到了一张比较Hibernate和MyBatis使用情况的图,顺手发了条朋友圈: Hibernate vs MyBatis ,谁能告诉我什么样的国情导致了这么大 ...

  2. Linux下汇编语言学习笔记50 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  3. 利用mysql分析小规模数据

    1 获取数据 示例:(/home/work/data/1.data) 123457,chenli,70 123458,liuyang,71 2 create table CREATE TABLE sc ...

  4. ZOJ 4016 Mergeable Stack 链表

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given  initially empty stacks, the ...

  5. poj-1979 && hdoj - 1312 Red and Black (简单dfs)

    http://poj.org/problem?id=1979 基础搜索. #include <iostream> #include <cstdio> #include < ...

  6. Ubuntu 16.04粘贴板增强工具Diodon

    相比Parcellite(http://www.cnblogs.com/EasonJim/p/7119308.html),Diodon可以支持图片. 安装: sudo add-apt-reposito ...

  7. Django学习系列之模板系统

    一.模板标签 if/else {%  if  %}标签检查一个变量的值是否为真或者等于另外一个值,如果为真,系统会执行{%  if  %}和{%  endif  %}之间的代码块,例如: {% if ...

  8. VB打开project时出现冲突名称提示

    在敲机房时因为窗口命名不合适,我就改动了下窗口.可是保存后再打开的时候.却出现以下的提示: 这样的情况出现.通常是因为引用了别人的窗口文件,或者是改动窗口的名字可是就是找不到不论什么冲突的地方.事实上 ...

  9. Django打造大型企业官网(四)

    4.3.轮播图布局和样式 templates/news/index.html <div class="news-wrapper"> <div class=&quo ...

  10. Android API Guides –System Permissions

    系统权限 声明: 本文由Gordon翻译 公布于www.dlvoice.com 欢迎转载,但请保留此声明 原文地址:http://developer.android.com/guide/topics/ ...