题目链接: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]的更多相关文章

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

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

  2. HDU 2612 Find a way BFS,防止超时是关键

    之前我写的时候是:每找到一个‘@’就广搜一次,如果这样写有多少个‘@’就会广搜几次,这样就超时了.我队友告诉我应该打个表,这个方法确实不错.因为'Y'和'M'是唯一的,我通过这两个点分别广搜一次,对所 ...

  3. HDU 2612 (2次BFS,有点小细节)

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  4. HDU.2612 Find a way (BFS)

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

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

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

  6. 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 ...

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

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

  8. 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,最先 ...

  9. HDU - 2612 Find a way 【BFS】

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

随机推荐

  1. 关于海康威视与Unity3d集成冲突问题解决

    一.集成 1.1 了解什么是ANSI系列与GNU系列    https://baike.baidu.com/item/ANSI%20C/7657277?fr=aladdin    https://ww ...

  2. 阿里云提示WordPress“/wp-includes/http.php输入IP验证不当”的解决办法

    本文转自:https://www.liuzhishi.com/2931.html 标题: wordpress IP验证不当漏洞 简介: wordpress /wp-includes/http.php文 ...

  3. Python之多线程和多进程

    一.多线程 1.顺序执行单个线程,注意要顺序执行的话,需要用join. #coding=utf-8 from threading import Thread import time def my_co ...

  4. Mybatis抛出:Cannot obtain primary key information from the database, generated objects may be incomplete

    使用 mybatis generator 生成pojo.dao.mapper时,可能会遇到 Cannot obtain primary key information from the databas ...

  5. Python 爬虫实例(9)—— 搜索 爬取 淘宝

    # coding:utf- import json import redis import time import requests session = requests.session() impo ...

  6. Elasticsearch索引mapping的写入、查看与修改(转)

    mapping的写入与查看 首先创建一个索引: curl -XPOST "http://127.0.0.1:9200/productindex" {"acknowledg ...

  7. 物联网架构成长之路(26)-Docker构建项目用到的镜像2

    0. 前言 前面介绍的都是一些标准的第三方中间件,基本都是有现成的Dockerfile或者Image,不需要我过多的关心,这一篇要介绍一些自己构建的Docker Image了.刚开始学,Dockerf ...

  8. Myeclipse安装、配置、测试

    Myeclipse安装.配置.测试(win7_64bit) 目录 1.概述 2.本文用到的工具 3.安装与激活 4.JavaSE开发测试(确保JDK已正确安装) 5.JavaEE开发测试(确保服务器和 ...

  9. Fluent动网格【12】:扩散光顺

    扩散光顺是Fluent提供的另外一种常用的网格光顺方法.其基本原理是通过求解扩散方程得到网格节点的运动位移. 扩散光顺基本计算 扩散光顺通过求解 以下扩散方程来设置网格的节点位置. \[ \nabla ...

  10. bash计算上下行数据差值

    for i in {1..60000}; do echo "`date +'%F %T'` `df /dev/md0 | grep 'data1'` "; sleep 1; don ...