题目代号: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. noip2011day2-观光公交

    题目描述 风景迷人的小城 \(Y\) 市,拥有 $n $个美丽的景点. 由于慕名而来的游客越来越多,\(Y\) 市特 意安排了一辆观光公交车,为游客提供更便捷的交通服务. 观光公交车在第 \(0\) ...

  2. 将ShellCode注入进程内存

    内存注入ShellCode的优势就在于被发现的概率极低,甚至可以被忽略,这是因为ShellCode被注入到进程内存中时,其并没有与之对应的硬盘文件,从而难以在磁盘中取证,但也存在一个弊端由于内存是易失 ...

  3. 图数据库:AgensGraph

    文章目录 AgensGraph简介 官网及下载 安装AgensGraph 上传并解压 添加agens用户 配置.bashrc 初始化并启动 初始化数据库 启动数据库 执行交互式终端 图数据库基础概念 ...

  4. RabbitMQ入门教程(十一):消息属性Properties

    原文:RabbitMQ入门教程(十一):消息属性Properties 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://b ...

  5. linux复习4:文件和目录

    7一.linux文件 1.linux文件的扩展名:文件扩展名是文件名最后一个点之后的部分,下面列出了其中一部分 (1)压缩文件和归档文件 压缩和归档的文件扩展名及其含义如下. .bz2:使用bzip2 ...

  6. 如何写一个 Burp 插件

    Burp 是 Web 安全测试中不可或缺的神器.每一个师傅的电脑里面应该都有一个 Burp.同时 Burp 和很多其他神器一样,它也支持插件.但是目前总体来说网上 Burp 插件开发的资料不是特别特别 ...

  7. js 学习四 对象应用 吃货游戏

    游戏来源于 Mdn学习网站: 该例子用于对象的理解非常有效(建议看完上面网站的内容在开始练习) 弹球 body { margin: 0; overflow: hidden; font-family: ...

  8. MySQL发生系统错误2 系统无法找到指定文件

    https://blog.csdn.net/digitalmon/article/details/78152187 https://www.cnblogs.com/gaogaoyanjiu/p/104 ...

  9. ZROI 19.07.30 简单字符串/ll

    写在前面:今天下午药丸--不会字符串,全程掉线/ll 给出字符串\(S\),\(q\)次询问,每次给出\(a,b,c,d\),询问\(S[a,b]\)的所有子串和\(S[c,d]\)最长公共前缀的最大 ...

  10. canvas 计算文字宽度(常用于文字换行)

    var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.font= ...