Find a way

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

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
 
题意:两个人去同一个kfc,问两个人一共走的最短路的合,kfc有多个
题解:每个人都要一次bfs,用一个kfc去找人会超时,先把所有的kfc都找到再处理
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=;
typedef pair<int,int >P;
int n,m;
int xa,xb,ya,yb;
char a[][];
int vis[][];
int d[][];
int d1[][];
struct fwe
{
int x,y;
}list[];
int dx[]={,,-,};
int dy[]={,,,-};
void bfs(int x,int y)
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
d[i][j]=INF;
queue<P>que; que.push(P(x,y));
d[x][y]=;
while(que.size())
{
P p=que.front(); que.pop();
for(int i=;i<;i++)
{
int nx=p.first+dx[i];
int ny=p.second+dy[i];
if(<=nx && nx<n && <=ny && ny<m && a[nx][ny]!='#' && d[nx][ny]==INF)
{
que.push(P(nx,ny));
d[nx][ny]=d[p.first][p.second]+;
}
} }
} void bfss(int x,int y)
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
d1[i][j]=INF;
queue<P>que;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
d1[i][j]=INF;
que.push(P(x,y));
d1[x][y]=;
while(que.size())
{
P p=que.front(); que.pop();
for(int i=;i<;i++)
{
int nx=p.first+dx[i];
int ny=p.second+dy[i];
if(<=nx && nx<n && <=ny && ny<m && a[nx][ny]!='#' && d1[nx][ny]==INF)
{
que.push(P(nx,ny));
d1[nx][ny]=d1[p.first][p.second]+;
}
} }
} int main()
{
int min;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
cin>>a[i][j];
if(a[i][j]=='Y')
{
xa=i;
ya=j;
}
if(a[i][j]=='M')
{
xb=i;
yb=j;
}
}
bfs(xa,ya);
bfss(xb,yb);
int minn=INF;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(a[i][j]=='@')
{
minn=std::min(minn,d[i][j]+d1[i][j]);
} }
cout<<minn*<<endl; }
return ;
}

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

  1. 非常可乐---hdu 1495(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意: 有3个杯子a b c:a=b+c:然后刚开始时只有a是满的,其它为空的,然后a b c三个之间互相 ...

  2. hdu 5012(bfs)

    题意:给你2个 骰子,让你通过翻转使第一个变成第二个,求最少翻转数 思路:bfs #include<cstdio> #include<iostream> #include< ...

  3. 逃离迷宫 HDU - 1728(bfs)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. ACM-ICPC 2017 沈阳赛区现场赛 G. Infinite Fraction Path && HDU 6223(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6223 参考题解:https://blog.csdn.net/qq_40482495/article/d ...

  5. 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)

    链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...

  6. HDU.2612 Find a way (BFS)

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

  7. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  8. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  9. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

随机推荐

  1. Kestrel服务器

    Kestrel服务器 什么是Kestrel服务器 Kestrel是开源的(GitHub提供的源代码),事件驱动的异步I / O服务器,用于在任何平台上托管ASP.NET应用程序.这是一个监听服务器和一 ...

  2. B -- RE:从零开始的异世界生活 线段树

    http://www.ifrog.cc/acm/problem/1117?contest=1016&no=1 其实我是第一次这样用线段树. 首先把所有出现过的数字全部离散化.那么数字就是从[1 ...

  3. Windows1

    ① 对Windows的设置一般在, 所有设置, 控制面板(control), 管理方式打开此电脑和此电脑上的选项 ② 关闭对账号安全的检验, 在控制面板中, 找到系统和安全, 再找到更改用户账号控制设 ...

  4. ruby 数组array 排序sort 和sort!

    1. sort → new_ary click to toggle source sort { |a, b| block } → new_ary Returns a new array created ...

  5. 有关在python中使用Redis(一)

    python作为一种处理数据的脚本语言本身有许多方法函数供大家使用,有时候为了提升数据处理速度(如海量数据的访问或者海量数据的读取),涉及分布式管理架构,可能需要用到Redis,Redis是一个开源的 ...

  6. 浅析HTML的元素类型及其转换

    大家都知道html是由标签元素组成的,在了解元素的类型转换之前,让我们先来了解一下html的元素类型. 一.html元素类型分为两种:块级元素和内联元素,内联元素又被称为行内元素.  常见的块级元素有 ...

  7. ABAP事件的简单用法

    1.1.事件: 用于捕获某类对象状态的改变来触发事件的方法,并进行处理 1.2.定义:可以在类或接口中进行声明 EVENTS|CLASS-EVENTS evt  EXPORTING … VALUE(p ...

  8. Android 自定义Android ORM 框架greenDAO数据库文件的路径

    import android.content.Context; import android.content.ContextWrapper; import android.database.Datab ...

  9. android studio项目提交Git@OSC

    转载地址:http://www.bubuko.com/infodetail-977061.html 先到git.oscchina.net网站上申请个账号,然后创建一个项目.过程不再说了. 新建工程后, ...

  10. uvm_reg_predictor——寄存器模型(十七)

    这是寄存器模型类中唯一派生自uvm_component的类,我们的寄存器模式需要实时,以最接近的方式知道DUT中寄存器的变化,uvm_reg_predictor就是为这个而生的. // TITLE: ...