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. 彻底理解mysql服务器的字符集转换问题

    主要参考这三个文章: https://www.xiariboke.com/article/4147.html http://blog.sina.com.cn/s/blog_690c46500100k1 ...

  2. 通过cmd调用Powershell脚本

    一共需要3个文件,把这3个文件放在一个路径下 UTF8NoBOM.bat   这个文件是为了调用ps1 pwsh -file "%cd%\UTF8NoBOM.ps1" UTF8No ...

  3. 论文笔记:Person Re-identification with Deep Similarity-Guided Graph Neural Network

    Person Re-identification with Deep Similarity-Guided Graph Neural Network 2018-07-27 17:41:45 Paper: ...

  4. [AtCode 4104] Small and Large Integers

    题目链接:https://abc093.contest.atcoder.jp/tasks/abc093_b?lang=en 这个题虽然很水,但是还是很容易踩坑,比如我,直接想到了[b,a]之间的长度和 ...

  5. [蓝桥] 基础练习 数列排序(java)

    问题描述 给定一个长度为n的数列,将这个数列按从小到大的顺序排列.1<=n<=200 输入格式 第一行为一个整数n. 第二行包含n个整数,为待排序的数,每个整数的绝对值小于10000. 输 ...

  6. Linux下 网卡测速

    参考: How do I verify the speed of my NIC? Linux下 网卡测速 命令: $ sudo ethtool eth0 Settings for eth0: Supp ...

  7. ngui处理不规则按钮点击

    吐个槽  棋牌类游戏做什么中国地图!!!  然后就要用到不规则按钮点击了 你懂的 213的unity虽然已经加入了polygoncollider 2d的支持 但是 但是 但是 是2d的 也就是说如果不 ...

  8. SNMOJ 31

    考虑将给入的$A$数组差分,得到差分数组$C$ 每一次操作相当于把差分数组的每一位${+1}$,其中一个位置上${-n+1}$. 我们可以直接算出要进行多少次操作:${T=\frac{\sum A[i ...

  9. P4391 [BOI2009]Radio Transmission

    描述: 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入格式: 第一行给出字符串的长度,1 < L ≤ 1,000,000. ...

  10. gcc 执行过程

    虽然我们称GCC是C语言的编译器,但使用gcc由C语言源代码文件生成可执行文件的过程不仅仅是编译的过程,而是要经历四个相互关联的步骤∶预处理(也称预编译,Preprocessing).编译(Compi ...