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.

InputThe 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

OutputFor 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
注意几个地方:1.必须两个人都要访问到那个肯德基才能用它更新答案。2.Y,M,肯德基都可以经过。3.搜到肯德基以后万万不能直接continue,因为肯德基能经过。万一有
..Y.
##@#
##@#
..M.这样的数据就会WA!!没注意到这里卡了一下午QnQ
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x;
int y;
int time;
};
int n,m;
char mmap[][];
int ans[][][];
bool vis[][];
const int INF=0x3f3f3f3f; int dir[][]={{,},{-,},{,},{,-}};
void bfs(int p,int sx,int sy)//p为0:Y p为1:M sx、sy为起点坐标
{
queue<node>q;
node start;
start.x=sx,start.y=sy,start.time=;
q.push(start);
vis[sx][sy]=;
while(q.size())
{
node pre;
pre=q.front();
q.pop();
if(mmap[pre.x][pre.y]=='@')
{
ans[pre.x][pre.y][p]=pre.time*;// p为0:Y花的时间 p为1:M花的时间
// continue;千万不能写continue!!!!
}
int i;
for(i=;i<=;i++)
{
int nx=pre.x+dir[i][];
int ny=pre.y+dir[i][];
if(!vis[nx][ny]&&nx>=&&nx<n&&ny>=&&ny<m&&mmap[nx][ny]!='#')
{
node nxt;
nxt.x=nx;
nxt.y=ny;
nxt.time=pre.time+;
q.push(nxt);
vis[nx][ny]=;
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(ans,,sizeof(ans));
memset(mmap,,sizeof(mmap));
memset(vis,,sizeof(vis));
int i,j;
for(i=;i<n;i++)
{
scanf("%s",mmap[i]);
}
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(mmap[i][j]=='Y')
{
memset(vis,,sizeof(vis));
bfs(,i,j);
}
else if(mmap[i][j]=='M')
{
memset(vis,,sizeof(vis));
bfs(,i,j);
}
}
}
int out=INF;
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(ans[i][j][]!=&&ans[i][j][]!=)out=min(out,ans[i][j][]+ans[i][j][]);//必须要两个人都能到kfc
}
}
cout<<out<<endl;
}
}
												

HDU2612 Find a way (跑两遍BFS)的更多相关文章

  1. Magic Potion(最大流,跑两遍网络流或者加一个中转点)

    Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...

  2. 求树的直径【两遍BFS】

    两遍BFS.从任意一个点出发,第一遍可以找到直径的一端,从这端出发即可找到另外一端. 证明:从U点出发,到达V[画个图便清晰了] 1.如果U在直径上,则V一定是直径的一个端点. 2.如果U不在直径上. ...

  3. 洛谷P1073最优贸易(跑两遍dij)

    题目描述 CC C国有n n n个大城市和m mm 条道路,每条道路连接这 nnn个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 mmm 条道路中有一部分为单向通行的道路,一部分为 ...

  4. POJ3268 Silver Cow Party (建反图跑两遍Dij)

    One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...

  5. CSU - 2031 Barareh on Fire (两层bfs)

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...

  6. P3119 [USACO15JAN]草鉴定Grass Cownoisseur 分层图或者跑两次最长路

    https://www.luogu.org/problemnew/show/P3119 题意 有一个有向图,允许最多走一次逆向的路,问从1再走回1,最多能经过几个点. 思路 (一)首先先缩点.自己在缩 ...

  7. 系统警告,Bronya请求支援(两遍最短路)

    系统警告,Bronya请求支援 Description 休伯利安号的一行人来到了由逆熵镇守的前文明遗迹[海渊城],他们准备用巨大的传送装置[海渊之眼]进入量子之海,寻找丢失的渴望宝石.然而在行动前夜爱 ...

  8. UVa 11624,两次BFS

    题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...

  9. HDU5886 Tower Defence 【两遍树形dp】【最长链预处理】

    题意:N个点的一棵带权树.切掉某条边的价值为切后两树直径中的最大值.求各个边切掉后的价值和(共N-1项). 解法一: 强行两遍dp,思路繁琐,维护东西较多: dis表示以i为根的子树的直径,dis2表 ...

随机推荐

  1. python 中if和elif的区别

    如果程序中判断事件很多,全部用if的话,会遍历整个程序,用elif 程序运行时,只要if或后续某一个elif之一满足逻辑值为True,则程序执行完对应输出语句后自动结束该轮if-elif(即不会再去冗 ...

  2. FLV文件格式分析(附源码)

    FLV文件主要由两部分组成:Header和Body. 1. Header header部分记录了flv的类型.版本等信息,是flv的开头,一般都差不多,占9bytes.具体格式如下: 文件类型 3 b ...

  3. 如何创建redis集群

    1.下载redis源码包 wget http://download.redis.io/releases/redis-3.2.4.tar.gz 2.解压并安装 tar xvf redis-.tar.gz ...

  4. 在spring Boot中使用swagger-bootstrap-ui(原文)

    1.swagger简介 Swagger是一个API接口管理工具,支持在线测试接口数据,根据配置自动生成API文档,结合spring mvc而提供界面化方法文档的一个开源框架. 1.1Swagger主要 ...

  5. AcWing 869. 试除法求约数

    #include <iostream> #include <algorithm> #include <vector> using namespace std; ve ...

  6. AcWing 870. 约数个数

    #include <iostream> #include <algorithm> #include <unordered_map> #include <vec ...

  7. Nuxt 常用的配置项

    1:在开发项目时 我们可能会遇到端口被占用或者指定IP的情况, 在Nuxt中 我们可以在page.json 文件中进行配置,例如希望IP配置成125.0.0.1,端口设置1616 "conf ...

  8. Java出现NoSuchElementException异常

    参考网址:https://blog.csdn.net/xiao_ma_csdn/article/details/78906650 出现这个异常是线程访问越界,这个时候就要检查下到底是哪里越界. 原因是 ...

  9. 一段代码实现Aplayer+网易云音乐接口

    玩cnblogs的小伙伴可以将以下代码复制到页脚html当中哦 <!-- require APlayer --> <link rel="stylesheet" h ...

  10. 【游戏体验】Buttonhunt(鼠标寻找按钮)

    虽然画风千奇百变 但这款益智游戏的趣味性相当高 推荐试玩 个人测评 游戏性 9/10 音乐 4/10 剧情 6/10 总评 19/30