Find a way

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
 

思路:两次BFS存下对每个kfc的最短距离,之后两两相加取min

代码:

 #include "cstdio"
#include "stdlib.h"
#include "iostream"
#include "algorithm"
#include "string"
#include "cstring"
#include "queue"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#define mj
#define db double
#define ll long long
using namespace std;
const int N=1e8+;
const int mod=1e9+;
//const ll inf=1e16+10;
#define inf 0x3f3f3f
typedef pair<int,int> P;
int n,m;
char s[][];
int d[][],k[][];
int dx[]={,,-,},dy[]={,,,-};
int t[][*];
int bfs(int sx,int sy,int id)
{
queue<P> q;
for(int i=;i<;i++){
for(int j=;j<;j++){
d[i][j]=N;
}
}
q.push(P(sx,sy));
d[sx][sy]=;
while(q.size()){
P p;
p=q.front(),q.pop();
for(int i=;i<;i++){
int nx=p.first+dx[i],ny=p.second+dy[i];
if(<=nx&&nx<n&&<=ny&&ny<m&&s[nx][ny]!='#'&&d[nx][ny]==N){
d[nx][ny]=d[p.first][p.second]+;
if(s[nx][ny]=='@') t[id][k[nx][ny]]=d[nx][ny];//到该点的距离
q.push(P(nx,ny));
}
}
}
return ;
}
int main()
{
int xx[],yy[];
while(scanf("%d%d",&n,&m)==){
memset(t,inf, sizeof(t));
int c=,cnt=,ma=N;
for(int i=;i<n;i++){
scanf("%s",s[i]);
for(int j=;j<m;j++){
if(s[i][j]=='Y') xx[c]=i,yy[c++]=j;
else if(s[i][j]=='M') xx[c]=i,yy[c++]=j;
else if(s[i][j]=='@'){
k[i][j]=cnt++;
}
}
}
bfs(xx[],yy[],),bfs(xx[],yy[],);
for(int i=;i<cnt;i++){
ma=min(t[][i]+t[][i],ma);
// printf("%d %d\n",t[0][i],t[1][i]);
}
printf("%d\n",*ma);
}
return ;
}

hdu 2612 多终点BFS的更多相关文章

  1. HDU.2612 Find a way (BFS)

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

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

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

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

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

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

  5. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  6. HDU 2612 Find a way【多起点多终点BFS/两次BFS】

    Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

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

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

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

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

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

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

随机推荐

  1. Flex 布局:语法篇

    网页布局(layout)是 CSS 的一个重点应用.布局的传统解决方案,基于盒状模型,依赖 display 属性 + position 属性 + float 属性.它对于那些特殊布局非常不方便,比如, ...

  2. 【小练习02】CSS--网易产品

    要求用css和HTML实现下图效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...

  3. 4.Node.js 微信消息管理

    一.写在前面的话   当用户发送消息给公众号时(或某些特定的用户操作引发的事件推送时),会产生一个POST请求,开发者可以在响应包(Get)中返回特定XML结构,来对该消息进行响应.   消息推送也是 ...

  4. Java经典编程题50道之四十一

    海滩上有若干个一堆桃子,五只猴子来分.第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份. 第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中, ...

  5. Spring学习(6)---Bean定义及作用域的例子

    (一)Bean的定义 先定义一个BeanAnnotation package com.mypackage; import org.springframework.stereotype.Componen ...

  6. 11.并发包阻塞队列之LinkedBlockingQueue

    在上文<10.并发包阻塞队列之ArrayBlockingQueue>中简要解析了ArrayBlockingQueue部分源码,在本文中同样要介绍的是Java并发包中的阻塞队列LinkedB ...

  7. 假如时光倒流,我会这样学习Java

    回头看看, 我进入Java 领域已经快15个年头了, 虽然学的也一般, 但是分享下我的心得,估计也能帮大家少走点弯路. [入门] 我在2001年之前是C/C++阵营, 有C和面向对象的基础, 后来转到 ...

  8. 格式化输出和printf命令

    GNU版本的printf命令用来格式化输出,效果类似与C语言的printf函数.2.x以上版本的Bash内建的printf命令和e/usr/bin下的printf命令使用方法一样. 例子:$print ...

  9. Java——面向对象基础

    Java继承 继承的概念 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类. 继承就是子类继承父类的特征和行为,使得子类具有父类的各种属性和方法,或子类从父类继承方法,使得子类具 ...

  10. mac os 安装 wget

    1. brew安装: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/inst ...