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. 在fragment中获取activity的组件

    在fragment中使用getActivity()即可获取activity的引用

  2. ***CodeIgnite/CI 去掉 index.php的 配置

    CI有效删除URL中的index.php 参考: http://codeigniter.org.cn/forums/thread-15444-1-1.html 读CI的使用手册的话,关于如何有效删除U ...

  3. Codeforces 651A Joysticks【贪心】

    题意: 两根操纵杆,每分钟操纵杆消耗电量2%,每分钟又可以给一个操纵杆充电1%(电量可以超过100%),当任何一个操纵杆电量降到0时,游戏停止.问最长游戏时间. 分析: 贪心,每次选择电量剩余最少的充 ...

  4. POJ——T 2728 Desert King

    http://poj.org/problem?id=2728 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27191   ...

  5. Linux下tomcat的catalina.out屏蔽

    修改catalina.sh ,找到下面的位置: if [ -z "$CATALINA_OUT" ] ; then#CATALINA_OUT="$CATALINA_BASE ...

  6. 输入一个URL之后。。。

    1.输入URL2.浏览器去浏览器缓存.系统缓存.路由器缓存查找缓存记录,有则直接访问URL对应的IP,无则下一步3.DNS解析URL,获得对应的IP4.浏览器通过TCP/IP三次握手连接服务器5.客户 ...

  7. 洛谷 P3137 [USACO16FEB]圆形谷仓Circular Barn_Silver

    P3137 [USACO16FEB]圆形谷仓Circular Barn_Silver 题目描述 Being a fan of contemporary architecture, Farmer Joh ...

  8. Servlet的HTTP状态码

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/http-status-codes.html: HTTP请求的格式和HTTP响应消息的格式是相似的 ...

  9. JSTL-格式标签库

    主页:http://www.cnblogs.com/EasonJim/p/6958992.html的分支页. 一.<fmt:formatNumber> <fmt:formatNumb ...

  10. (linux shell)第一章--小试牛刀(下)

    文章来源: (linux shell)第一章--小试牛刀(下) 1.6 数组和关联数组 1.6.1 预备知识 Bash同一时候支持普通数组和关联数组.普通数组仅仅能使用整数作为数组索引,而关联数组能够 ...