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 思路:典型的BFS,两次BFS并记录步数。
AC Code:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#include<utility>
using namespace std;
typedef pair<int,int> P;
int step[][]= {,,-,,,,,-};
bool vis[][];
char map[][];
int stepnum[][][];
int n,m,INF=0x3f3f3f3f;
struct node
{
P p;
int step;
};
int check(int x,int y)
{
if(x>=&&x<=n&&y>=&&y<=m&&map[x][y]!='#')
return ;
return ;
}
void BFS(int a,P p)
{
memset(vis,false,sizeof(vis)); vis[p.first ][p.second]=true;
node point,newpoint; queue<node> q;
point.p .first=p.first ;
point.p .second=p.second ;
point.step=;
q.push(point); while(!q.empty())
{
point=q.front();
q.pop();
if(map[point.p.first][point.p.second ]=='@')//遇到KFC 记录所走的步数
{
stepnum[a][point.p.first ][point.p.second ]=point.step;
}
for(int i=; i<; i++)
{
newpoint.p.first =point.p.first+step[i][];
newpoint.p.second =point.p.second +step[i][];
if(check(newpoint.p.first,newpoint.p.second)&&!vis[newpoint.p.first][ newpoint.p.second])
{
vis[newpoint.p.first][ newpoint.p.second]=true;
newpoint.step=point.step+;//每走一格 步数+1
stepnum[a][newpoint.p.first][newpoint.p.second]=newpoint.step;
q.push(newpoint);
}
}
}
}
int main (void)
{
while(~scanf("%d%d",&n,&m))
{
P Y,M;
getchar();
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='Y')
Y=make_pair(i,j);
else if(map[i][j]=='M')
M=make_pair(i,j);
}
getchar();
}
memset(stepnum,INF,sizeof(stepnum));//一定要放在BFS之前 如果放在BFS里面,第二次BFS时,会把第一次BFS的值覆盖 结果出错
BFS(,Y);
BFS(,M);
int step_num=INF;
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
if(map[i][j]=='@')//找出同一个KFC中需要走最小的那个点
step_num=min(step_num,stepnum[][i][j]+stepnum[][i][j]);
printf("%d\n",step_num*);
}
return ;
}

Find a way HDU - 2612的更多相关文章

  1. HDU 2612 Find a way(找条路)

    HDU 2612 Find a way(找条路) 00 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)   Problem  ...

  2. HDU.2612 Find a way (BFS)

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

  3. BFS(最短路) HDU 2612 Find a way

    题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...

  4. HDU 2612 Find a way(双向bfs)

    题目代号:HDU 2612 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 M ...

  5. hdu 2612 Find a way

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Description Pass a year learning in H ...

  6. HDU 2612 - Find a way - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...

  7. HDU 2612 Find a way bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...

  8. (广搜) Find a way -- hdu -- 2612

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others) ...

  9. HDU - 2612 Find a way 【BFS】

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

  10. HDU 2612 (BFS搜索+多终点)

    题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...

随机推荐

  1. HDU 1848 Fibonacci again and again【博弈SG】

    Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)=2; F(n)=F(n-1)+F( ...

  2. LightOJ 1030 Discovering Gold(概率DP)题解

    题意:1~n每格都有金子,每次掷骰子,掷到多少走几步,拿走那格的金子,问你金子的期望 思路:dp[i]表示从i走到n金子的期望,因为每次最多走1<=x<=6步,所以dp[i] = a[i] ...

  3. 好用的js模板

    组织form下的 json对象 $.fn.serializeObject = function() { var o = {"unique_id":new Date().getTim ...

  4. BZOJ 3622 已经没有什么好怕的了

    扯淡 看到题目想到二项式反演 然后忘了给求阶乘的时候取模,调了一晚上 真令人窒息 思路 二项式反演 首先二项式反演还有另一种形式(不会证) 设\(G_i\)为有至少i个的方案数量,\(F_i\)为恰好 ...

  5. The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE)

    The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE) Today I tried ...

  6. [ECharts] - ECharts使用中国地图

    格式1: https://www.cnblogs.com/luna666/p/9007263.html  (非官方) <!DOCTYPE html> <html lang=" ...

  7. Dependency Injection2

    IoC容器和Dependency Injection 模式   使用 Service Locator 依赖注入的最大好处在于:它消除了MovieLister类对具体 MovieFinder实现类的依赖 ...

  8. 【译】第18节---数据注解-ForeignKey

    原文:http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-fi ...

  9. jmeter命令行模式运行,实时获取压测结果

    jmeter命令行模式运行,实时获取压测结果 jmeter很小,很快,使用方便,可以在界面运行,可以命令行运行.简单介绍下命令行运行的方式: sh jmeter.sh -n -t my-script. ...

  10. 基于 Python 和 Pandas 的数据分析(5) --- Concatenating and Appending

    这一节我们将会介绍几种不同的合并数据的方法. 在我们这个不动产投资的例子中, 我们希望获取 51 个州的房产数据, 并把它们组合起来. 我们这样做有很多原因. 这样做既便于我们做分析, 同时也可以占用 ...