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. mysql之 binlog维护详细解析(开启、binlog相关参数作用、mysqlbinlog解读、binlog删除)

    binary log 作用:主要实现三个重要的功能:用于复制,用于恢复,用于审计.binary log 相关参数:log_bin设置此参数表示启用binlog功能,并指定路径名称log_bin_ind ...

  2. js实现防盗图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. java GUI (课堂笔记)

    关于java GUI Swing组件: JFrame 窗口组件 JLabel 标签 JButton 按钮组件 JTextField 单行文本框 系统控件(JDK自带) 自定义控件(模仿系统控件): 继 ...

  4. springboot问题:解决异常Unable to start embedded container;

    使用eclipse创建springboot练习时,当主函数与控制器同时写在同一个类时,启动项目正常运行,而当把主函数单独放在一个类中时,无论是与控制器同包还是控制器所在的包是其子包,都报: org.s ...

  5. python读写文件相关内容

    1.python读取文件 f = open('E:/info.txt', 'r')#用read的方式打开a = 0for line in f.readlines():读取没一行,就是读取所有文件的意思 ...

  6. SVN·最新使用教程总结

    SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...

  7. 安装Mysql5.7并修改初始密码

    Centos 安装MySQL可以参考之前写的一篇文章 Centos7.3 安装Mysql5.7并修改初始密码 windows安装mysql5.7有两种方式 1.下载.msi安装文件直接根据界面提示进行 ...

  8. 信号处理——卷积(convolution)的实现

    作者:桂. 时间:2017-03-07  22:33:37 链接:http://www.cnblogs.com/xingshansi/p/6517301.html 前言 信号时域.频域对应关系,及其D ...

  9. Myeclipse中隐藏jar包

    在package explorer的右上角有一个向下的小三角 点击选择Filter 在打开的对话框中 第一个选框中打上对勾 文字框中填上 *.jar 然后点击OK就行了 多个隐藏内容之间用逗号隔开 如 ...

  10. java web数据库(SQL 2008+IDEA 14)环境配置

    废话少说,在之前已经配置过IDEA+Tomcat的环境之后,现在需要进行数据库配置: 1.首先,SQL SERVER2008数据库的安装 (1)将下载的sqlserver 2008数据库进行解压,点击 ...