题目代号: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. Go语言代码结构与语法基础(二)

    任何一门语言,都是从打印 hello world 开始的. 最简单的go代码: package main // 声明 main 包,表明当前是一个可执行程序 import "fmt" ...

  2. js and java 中正则表达式的使用

    首先介绍一下js当中的几个关键的正则表达式: 1.js中的正则表达式校验 a: RegExp(如果这里有转义字符的话,需要使用“\\”) var patt1=new RegExp("e&qu ...

  3. 洛谷 P2467 地精部落 题解

    题面 好难啊好难啊好难啊~(以后再玩魔兽的时候绝对绝对虐死他) 做完后总结了一下思路; 首先推一下以下三条性质: 1.若两个 i 与 i+1 不相邻,那么我们直接交换这两个数字就可以组成一个新的数列 ...

  4. PHP手册在7.1迁移页面给出了替代方案,就是用OpenSSL取代MCrypt.

    /**  * [AesSecurity aes加密,支持PHP7.1]  */ class AesSecurity {     /**      * [encrypt aes加密]      * @p ...

  5. jumpserver-1.4.8安装步骤

    1. 组件说明 Jumpserver 为管理后台, 管理员可以通过 Web 页面进行资产管理.用户管理.资产授权等操作, 用户可以通过 Web 页面进行资产登录, 文件管理等操作 koko 为 SSH ...

  6. AQS之Condition

    一.引言 一般我们在使用锁的Condition时,我们一般都是这么使用,以ReentrantLock为例, ReentrantLock lock = new ReentrantLock(); Cond ...

  7. js 学习二 字符串常用方法

    1.字符串长度 string.length var browserType = 'mozilla'; browserType.length; //7 2在字符串中查找子字符串 string.index ...

  8. 自己对GIS的思考

    这只是我自己的理解,谈不上对整个行业的理解,只能从自己的角度谈谈GIS,谈谈爱和恨. 现在在武汉的一所所谓的全国GIS数一数二的学校里面读硕士,从高中开始我就很喜欢地理学科,大学选择了地球信息科技这个 ...

  9. Delphi 条件语句和程序的选择结构

  10. yum 报错2

    Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again 打开/etc/yu ...