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. Linux下汇编语言学习笔记24 ---

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

  2. codevs——1080 线段树练习

    1080 线段树练习  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 一行N个方格,开始每个格子里都有 ...

  3. CE310A

    http://v.ku6.com/show/tvWNTLZTVWuGVPE5ZMSUyQ...html

  4. java多线程断点下载原理(代码实例演示)

    原文:http://www.open-open.com/lib/view/open1423214229232.html 其实多线程断点下载原理,很简单的,那么我们就来先了解下,如何实现多线程的断点下载 ...

  5. 如何启动/关闭weblogic

    听语音 | 浏览:7107 | 更新:2014-12-25 15:43 1 2 3 4 5 6 分步阅读 最近用到weblogic 给大家分享一下如何启动和关闭weblogic 工具/原料   电脑 ...

  6. TP-Link的Atheros芯片的WR886n v5 安装SuperWRT系统

    安装SuperWRT系统 本教程以TP-Link的Atheros芯片的WR886n v5为例,教新手如何刷入一个已支持设备的固件. 下载设备固件请访问:这里 (没有支持你的设备?自由动手一下:hack ...

  7. C#写的NoSQL开源项目/系统(系列)

    http://www.cnblogs.com/unruledboy/archive/2013/01/07/CSharpNoSQL.html 闲扯 好久没写开源项目了,也没写对新开源项目的介绍,今晚看了 ...

  8. 005 EIGRP

    Router>en Router#config t Enter configuration commands, one per line.  End with CNTL/Z. Router(co ...

  9. hdu1025 Constructing Roads In JGShining&#39;s Kingdom(二分+dp)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem ...

  10. 网上Unused Index Script 脚本的问题

    曾经使用过网上下载的脚本查询没有使用过的Index比方SQL SERVER – 2008 – Unused Index Script – Download,事实上如今看起来这个脚本是有一些问题. 脚本 ...