HDU 2612 - Find a way - [BFS]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612
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
题意:
两人分别从 "Y" 和 "M" 出发,在地图上走,要找一个 "@" 碰头,每走一格花费时间 $11$ 分钟,求最短的碰头时间。
题解:
以两个人分别为起点做BFS,求出两人到每家KFC的各自花费的时间。
对全部KFC维护两人到达时间之和的最小值即为答案。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MAX=;
const int dx[]={,,,-};
const int dy[]={,,-,};
int n,m;
char mp[MAX][MAX];
pii Y,M;
int KFCcnt;
map<pii,int> KFC;
int Time[][MAX*MAX]; queue<pii> Q;
int d[MAX][MAX];
void bfs(pii st,int t)
{
memset(Time[t],0x3f,sizeof(Time[t]));
memset(d,-,sizeof(d));
Q.push(st); d[st.first][st.second]=;
while(!Q.empty())
{
pii now=Q.front(); Q.pop();
if(mp[now.first][now.second]=='@')
Time[t][KFC[now]]=d[now.first][now.second];
for(int k=;k<;k++)
{
pii nxt=now; nxt.first+=dx[k], nxt.second+=dy[k];
if(mp[nxt.first][nxt.second]=='#') continue;
if(~d[nxt.first][nxt.second]) continue;
Q.push(nxt);
d[nxt.first][nxt.second]=d[now.first][now.second]+;
}
}
}
int main()
{
for(int i=;i<MAX;i++) mp[i][]=mp[][i]='#';
while(cin>>n>>m)
{
KFCcnt=; KFC.clear();
for(int i=;i<=n;i++)
{
scanf("%s",mp[i]+), mp[i][m+]='#';
for(int j=;j<=m;j++)
{
if(mp[i][j]=='@') KFC[make_pair(i,j)]=++KFCcnt;
if(mp[i][j]=='Y') Y=make_pair(i,j);
if(mp[i][j]=='M') M=make_pair(i,j);
}
}
for(int j=;j<=m+;j++) mp[n+][j]='#'; bfs(Y,);
bfs(M,);
int res=0x3f3f3f3f;
for(int i=;i<=KFCcnt;i++) res=min(res,Time[][i]+Time[][i]);
printf("%d\n",res);
}
}
HDU 2612 - Find a way - [BFS]的更多相关文章
- HDU 2612 Find a way bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...
- HDU 2612 Find a way BFS,防止超时是关键
之前我写的时候是:每找到一个‘@’就广搜一次,如果这样写有多少个‘@’就会广搜几次,这样就超时了.我队友告诉我应该打个表,这个方法确实不错.因为'Y'和'M'是唯一的,我通过这两个点分别广搜一次,对所 ...
- HDU 2612 (2次BFS,有点小细节)
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- BFS(最短路) HDU 2612 Find a way
题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...
- 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 ...
- HDU 2612 Find a way(找条路)
HDU 2612 Find a way(找条路) 00 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- HDU - 2612 Find a way 【BFS】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意 有两个人 要去一个城市中的KFC 一个城市中有多个KFC 求两个人到哪一个KFC的总时间最 ...
随机推荐
- Android 实现登录界面和功能实例
近期一个android小程序须要登录功能,我简单实现了一下.如今记录下来也当做个笔记,同一时候也希望能够相互学习.所以,假设我的代码有问题,还各位请提出来.多谢了! 以下.就简述一下此实例的主要内容: ...
- Unty中通过镜像优化HDRI全景图体积
全景图即HDRI贴图,可以代替6面cubemap,传统3D软件运用较为广泛.一般反射探针,天空盒等都会用到. 但是体积过大是个问题,特别是移动端会对包体大小进行控制,虽说可以通过球面贴图替换掉部分环境 ...
- C#中[WebMethod]的用法,aspx、ashx、asmx
在.net 3.5的情况下 前台JQuery做Ajax的时候,服务器端 (1)可以调用aspx.cs 中声明带有[WebMehtod]的public static 的方法(不需要自己手动添加web.c ...
- Python中_,__,__xx__的区别
_xx 单下划线开头 Python中没有真正的私有属性或方法,可以在你想声明为私有的方法和属性前加上单下划线,以提示该属性和方法不应在外部调用.如果真的调用了也不会出错,但不符合规范. #! /usr ...
- k8s namespace/volume
https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/ 只挑个人感觉使用较多/比较重要的点来说 ...
- YAML文件格式_k8s/docker-compose配置文件
YAML(Yet Another Markup Language),是一个JSON的超集,意味着任何有效JSON文件也都是一个YAML文件.它规则如下: )大小写敏感 )使用缩进表示层级关系,但不支持 ...
- 【规范】前端编码规范——css 规范
编码 在 css 首行设置文件编码为 UTF-8. @charset "UTF-8"; class 命名 class 名称应当尽可能短,并且意义明确.使用有意义的名称,使用有组织的 ...
- Sigmoid函数简介
Sigmoid函数是一个在生物学中常见的S型的函数,也称为S型生长曲线.[1] 中文名 Sigmoid函数 外文名 Sigmoid function 别名 S型生长曲线 Sigmoid函数由下列公式定 ...
- python -- Pythonic
所谓Pythonic,就是极具Python特色的Python代码(明显区别于其它语言的写法的代码) 总结如下: 两变量的内容交换 Python:a,b = b,a 非Python:t=a;a=b;b= ...
- makefile 常用函数
Linux下编译c/c++源码需要编写makefile文件,文章参看 http://blog.sina.com.cn/s/blog_4c4d6e74010009jr.html 一函数的调用语法 二字符 ...