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. Corn 表达式

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...

  2. 持续集成~Jenkins构建GitHub项目的实现

    有了前两讲的基础,这回我们就可以把github上的项目做到CI(jenkins)里了,让它自动去集成部署,持续集成~Jenkins里的NuGet和MSBuild插件,持续集成~Jenkins里的pow ...

  3. SpringBoot整合Redis使用Restful风格实现CRUD功能

    前言 本篇文章主要介绍的是SpringBoot整合Redis,使用Restful风格实现的CRUD功能. Redis 介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-valu ...

  4. java类在eclipse上打jar包,Linux上成功运行的实例

    1 eclipse下的java项目结构如下图所示: 2 打包的步骤如下: 3 修改minifest.mf文件:  4 .上传需要的三方jar包们和主类打的jar(案例是topV.jar)并且执行jav ...

  5. Dev控件工具箱安装

    安装目录\Components\Tools,打开命令行 安装DEV工具 $ ToolboxCreator.exe /ini:toolboxcreator.ini 移除DEV工具 $ ToolboxCr ...

  6. mysql 中unsigned

    整型的每一种都分有无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在默认情况下声明的整型变量都是有符号的类型(char有点特别),如果需声明无符号类 ...

  7. java中循环的不同终止方式

    1.break:直接强行跳出当前循环,不再执行剩余代码.但在多重循环的情况下,若break在内层循环中,则仅仅终止了内层循环,外循环照常执行. 2.continue:仅仅终止此次循环. 3.retur ...

  8. IT届常用单词读法纠正

    Bootstrap    ['bʊt'stræp] Java  ['dʒɑːvə] Node           [nod] @  [æt; ət] Common      ['kɑmən] Java ...

  9. apache官方供下载所有项目所有版本的官方网站地址

    Apache官网有一个列举apache所有发布的项目的各个版本的官方网站,现在在此记录下来供大家快速浏览使用. 网站地址如下: http://archive.apache.org/dist/

  10. Exchange DSAccess 事件分析

    本文介绍了如何使用事件 ID 2080 来帮助诊断 Exchange DSAccess 问题中所包含的信息. 许多朋友经常遇到Active Directory 域和Exchange 服务器通信问题.那 ...