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. SpringBoot---Web开发---WebSocket

    [广播式] 1. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...

  2. 076 Minimum Window Substring 最小窗口子字符串

    给定一个字符串 S 和一个字符串 T,找到 S 中的最小窗口,它将包含复杂度为 O(n) 的 T 中的所有字符.示例:S = "ADOBECODEBANC"T = "AB ...

  3. 《springcloud 三》分布式配置中心

    Git环境搭建 使用码云环境搭建git服务器端 码云环境地址:https://gitee.com/majie2018 服务端详解 项目名称:springboot2.0-config_server Ma ...

  4. docker安装软件

    镜像相关命令 1.搜索镜像 # docker search java 可使用 docker search命令搜索存放在 Docker Hub(这是docker官方提供的存放所有docker镜像软件的地 ...

  5. xxx cannot be resolved to a type

    1.jdk不匹配(或不存在)     项目指定的jdk为“jdk1.6.0_18”,而当前eclipse使用的是“jdk1.6.0_22”.需要在BuildPath | Libraries,中做简单调 ...

  6. DOM所有的命令(CMD)

    刚接触电脑的时候是从DOS系统开始,DOS时代根本就没有Windows这样的视窗操作界面,只有一个黑漆漆的窗口,让你输入命令.所以学DOS系统操作,cmd命令提示符是不可或缺的.可以告诉大家,大多数的 ...

  7. JAVA 员工管理系统(用抽象类实现),简易版。

    package Demo513; /* 定义一个Employee类,该类包含: private 成员变量name,number,birthday,其中birthday为MyDate类的对象: abst ...

  8. java http的get,post请求

    初学可用F12查看任意网页帮助理解 package httpTest: import java.io.BufferedReader; import java.io.IOException;import ...

  9. JavaScript判断图片是否已经加载完毕的方法汇总

    在网上有很多关于判断图片是否已经加载完毕的文章,但是有的浏览器并不适合,下面小编给大家分享一些有关JavaScript判断图片是否已经加载完毕方法汇总,具体内容如下所示: 一.onload事件 通过监 ...

  10. POJ 3254 Corn Fields (状压DP,轮廓线DP)

    题意: 有一个n*m的矩阵(0<n,m<=12),有部分的格子可种草,有部分不可种,问有多少种不同的种草方案(完全不种也可以算1种,对答案取模后输出)? 思路: 明显的状压DP啦,只是怎样 ...