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. Pull方式解析XML文件

    package com.pingyijinren.test; import android.content.Intent; import android.os.Handler; import andr ...

  2. MySQL使用教程收集(语法教程/命令教程)

    说明:现在市面上的教程除了基本语法外,都基本是五花八门的,最权威且最全面的解释应该上官网去查看. https://www.tutorialspoint.com/mysql/index.htm http ...

  3. 保持WCF服务端与客户端的长连接

    背景 客户端与服务端使用WCF建立连接后:1.可能长时间不对话(调用服务操作):2.客户端的网络不稳定. 为服务端与客户端两边都写“心跳检测”代码?不愿意. 解决 设置inactivityTimeou ...

  4. html自动换行

    对于div,p等块级元素 正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义的宽度之后自动换行html css 1.(IE浏览器)连续的英文字符和阿拉伯数 ...

  5. 苹果Macbook Air怎么安装Win7系统图解教程

    下面开始我们在苹果Macbook Air上的Windows7之旅吧.

  6. 我的arcgis培训照片7

    来自:http://www.cioiot.com/successview-553-1.html

  7. ajax请求锁屏功能

    我们有时候在进行ajax请求的时候希望页面不允许点击,等请求结束之后才可以进行点击,那么可以写: $(".cloudos-container").ajaxStart($.block ...

  8. UGUI 实现Button长按效果(RepeatButton)

    Tag:加入了一个延迟,在button按下状态一段时间后再開始 repeate using UnityEngine; using UnityEngine.Events; using UnityEngi ...

  9. [RK3288][Android6.0] 音频调试方法小结【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/70053135 Platform: ROCKCHIPOS: Android 6.0Kernel ...

  10. 【NOIP 2016】初赛-完善程序 & 参考答案

    参考答案 感觉这两题目都挺好的~~ T1 交朋友 简单描述:有n个人依次进入教室,每个人进入会找一个身高绝对值相差最小的人交朋友(相同时更想和高的交朋友),求每个人交的朋友. Solution: So ...