题目代号:HDU 2612

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15919    Accepted Submission(s): 5110

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遍历所有的位置,分别用两个数组作为标记,如果碰到@就保存当前花费的步数,然后两个数组对应位置相加,然后找到总步数最小的那个的值乘以11,输出就行了

AC代码:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL; const int MAXM=;
char a[MAXM][MAXM];
int n,m;
int cx[]= {-,,,};
int cy[]= {,,-,}; struct node
{
int x,y;
int flag;
int step;
}; queue<node>Q;
int vis1[MAXM][MAXM];
int vis2[MAXM][MAXM]; void bfs()
{
while(!Q.empty())
{
int x=Q.front().x;
int y=Q.front().y;
int flag=Q.front().flag;
int cnt=Q.front().step;
Q.pop();
for(int i=; i<; i++)
{
int tx=x+cx[i];
int ty=y+cy[i];
if(flag==)
{
if(vis1[tx][ty]==-)
{
if(a[tx][ty]=='@')
{
vis1[tx][ty]=cnt+;
}
else
{
vis1[tx][ty]=;
}
Q.push(node{tx,ty,,cnt+});
}
}
else
{
if(vis2[tx][ty]==-)
{
if(a[tx][ty]=='@')
{
vis2[tx][ty]=cnt+;
}
else
{
vis2[tx][ty]=;
}
Q.push(node{tx,ty,,cnt+});
}
}
}
}
} int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d",&n,&m))
{
mem(vis1,);
mem(vis2,);
for(int i=; i<=n; i++)
{
scanf("%s",a[i]+);
for(int j=; j<=m; j++)
{
if(a[i][j]=='Y')
{
Q.push(node{i,j,,});
vis1[i][j]=vis2[i][j]=-;
}
else if(a[i][j]=='M')
{
Q.push(node{i,j,,});
vis1[i][j]=vis2[i][j]=-;
}
else if(a[i][j]=='.'||a[i][j]=='@')
{
vis1[i][j]=vis2[i][j]=-;
}
}
}
bfs();
/*
FOR(i,1,n)
{
FOR(j,1,m)
{
printf("%3d",vis1[i][j]);
}
puts("");
}
puts("");
FOR(i,1,n)
{
FOR(j,1,m)
{
printf("%3d",vis2[i][j]);
}
puts("");
}
*/
int ans=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
vis1[i][j]+=vis2[i][j];
if(vis1[i][j]>)ans=min(ans,vis1[i][j]);
}
}
cout<<ans*<<endl;
}
return ;
}

HDU 2612 Find a way(双向bfs)的更多相关文章

  1. HDU.2612 Find a way (BFS)

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

  2. HDU——1195Open the Lock(双向BFS)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDU/HDOJ 2612 Find a way 双向BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 思路:从两个起点出发,有多个终点,求从两个起点同时能到达的终点具有的最小时间,开两个数组分别保存 ...

  4. HDU - 2612 Find a way 【BFS】

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

  5. hdu 2612 Find a way(BFS)

    题目链接:hdu2612 思路:题意是求两个人到某一个KFC花费时间和最小,其实就是求最短距离和,用两个BFS,分别以两个人为起点,分别记录下两人到每个KFC的距离,然后求出最小的和 #include ...

  6. (简单) HDU 2612 Find a way,BFS。

    Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...

  7. HDU - 2612 Find a way(BFS搜索)

    题目: 链接 思路: 用BFS分别以‘Y’和‘M’的位置为起点进行两次搜索,并把这两次的搜索结果在一个二维数组中保存下来,在对地图遍历遇到‘@’更行最小值. PS: 如果用‘Y’和‘M’点分别去搜每个 ...

  8. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  9. 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...

随机推荐

  1. Selenium+PhantomJs 爬取网页内容

    利用Selenium和PhantomJs 可以模拟用户操作,爬取大多数的网站.下面以新浪财经为例,我们抓取新浪财经的新闻版块内容. 1.依赖的jar包.我的项目是普通的SSM单间的WEB工程.最后一个 ...

  2. Spring boot启动后没有生成日志文件问题排错

    我的配置是: logging.file.name=spring-boot.log logging.file.path=D:/log/ 系统启动后日志文件没有生成 原因:一开始以为这两个属性是配合着使用 ...

  3. numpy数组转置与轴变换

    numpy数组转置与轴变换 矩阵的转置 >>> import numpy as np >>> arr=np.arange(15).reshape((3,5)) &g ...

  4. 小白学习django第三站-自定义过滤器及标签

    要使用自定义过滤器和标签,首先要设置好目录结构 现在项目目录下建立common的python包 再将common加入到setting.py中的INSTALLED_APP列表中 在common创建目录t ...

  5. Jade学习(二)之语法规则上

    语法 ⚠️实例均结合node jade缩进代表层级 html <html></html> html <html> head <head> style & ...

  6. ubuntu 安装mysql5.7

    一.Windows mysql5.6 解压版 安装 关于widnows平台上的安装教程,可参考百度经验: 链接:https://jingyan.baidu.com/article/f3ad7d0ffc ...

  7. 移除数组中指定键(Yii2)

    /** * 移除数组中指定key * @param $data * @param $key * @return array */ public static function removeKey($d ...

  8. JS 的 Document对象

    Document 对象是是window对象的一个属性,因此可以将document对象作为一个全局对象来访问. 当浏览器载入 HTML 文档, 它就会成为 Document 对象. Document对象 ...

  9. Java学习路线(完整详细版)

    Java学习路线(完整详细版) https://jingyan.baidu.com/article/c1a3101e110864de656deb83.html

  10. angular集成tinymce

    1.前言 我使用的是angular的7.x版本,和之前的低版本集成方式有所区别.这里记录下基本的集成步骤. 2.集成步骤 2.1安装tinymac包 npm install tinymce --sav ...