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. hdfoo站点开发笔记-2

    httpd的目录的 Options: (里面的单词都是用的复数): Options Indexes FollowSymLinks 为了避免有些目录下没有生成deny.htm而显示列表, 可以直接给 / ...

  2. 【做题】Codeforces Round #436 (Div. 2) F. Cities Excursions——图论+dfs

    题意:给你一个有向图,多次询问从一个点到另一个点字典序最小的路径上第k个点. 考虑枚举每一个点作为汇点(记为i),计算出其他所有点到i的字典序最小的路径.(当然,枚举源点也是可行的) 首先,我们建一张 ...

  3. centos6.5下安装jdk并配置环境变量

    链接: https://blog.csdn.net/wawawawawawaa/article/details/81158943 以下链接供参考: https://blog.csdn.net/Bugg ...

  4. P2475 [SCOI2008]斜堆(递归模拟)

    思路 可并堆真是一种神奇的东西 不得不说这道题是道好题,虽然并不需要可并堆,但是能加深对可并堆的理解 首先考虑斜堆的性质,斜堆和左偏树相似,有如下的性质 一个节点如果有右子树,就一定有左子树 最后插入 ...

  5. Linux命令1——a

    addUser: -c:备注 -d:登陆目录 -e:有效期限 -f:缓冲天数 -g:组 -b:用户目录 -G:附加组 -s:制定使用默认的shell -u:指定用户ID -r:建立系统账号 -M:不自 ...

  6. 1. AMQP 0-9-1模型简介(官网直译)

    关于这篇指导文档 本文提供了AMQP 0-9-1协议的一个概述,它是RabbitMQ所支持的协议之一. AMQP 0-9-1是什么 AMQP 0-9-1(Advanced Message Queuin ...

  7. JMeter 生成CSV文件中文变乱码的问题

    在通过BeanShell 生成CSV文件时,写入的中文字符默认情况会变成乱码. //默认情况生成的文件是asii编码.fileName = “c:\test.csv";fos = new F ...

  8. spring读取bean有几种方式

    bean加载到spring的方式: 第一种:xml 第二种:注释「一定要配合包扫描」: <context:component-scan base-package="Cristin.Co ...

  9. 【Mysql】key 、primary key 、unique key 与index区别

    参考:https://blog.csdn.net/nanamasuda/article/details/52543177 总的来说,primary key .unique key 这些key建立的同时 ...

  10. sklearn.linear_model.LinearRegression

    官网:http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html class ...