题目链接: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): 17047    Accepted Submission(s): 5486

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
 
Author
yifenfei
 
Source

题解:

开个数组记录两个人到达某个KFC的时间和,然后两人分别进行一次BFS。

坑点: 在bfs完然后更新答案的时候,遇到KFC不一定意味着能更新答案,因为这个KFC可能只有一个人或没有人到达,所以还有开多个数组,记录每个KFC有多少个人能够到达。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; char M[MAXN][MAXN];
int vis[MAXN][MAXN], time[MAXN][MAXN], arrived[MAXN][MAXN]; //time为两人到达某个KFC的时间和,arrived为某个KFC的到达人数
int n, m, dir[][] = {,,,,,-,-,}; struct node
{
int x, y, step;
}; queue<node>que;
void bfs(int x, int y)
{
ms(vis,);
while(!que.empty()) que.pop(); node now, tmp;
vis[x][y] = ;
now.x = x, now.y = y, now.step = ;
que.push(now); while(!que.empty())
{
now = que.front();
que.pop(); for(int i = ; i<; i++)
{
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
if(tmp.x>= && tmp.x<=n && tmp.y>= && tmp.y<=m && !vis[tmp.x][tmp.y] && M[tmp.x][tmp.y]!='#' )
{
vis[tmp.x][tmp.y] = ;
tmp.step = now.step + ;
que.push(tmp);
if(M[tmp.x][tmp.y]=='@')
{
arrived[tmp.x][tmp.y]++; //多了一个人到达
time[tmp.x][tmp.y] += tmp.step; //更新时间之和
}
}
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%s", M[i]+); ms(time,);
ms(arrived,);
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
if(M[i][j]=='M' || M[i][j]=='Y')
bfs(i,j); int ans = INF;
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
if(M[i][j]=='@' && arrived[i][j]==) //遇到KFC且两个人都能到达
ans = min(ans, time[i][j]); printf("%d\n",ans*);
}
}

HDU2612 Find a way —— BFS的更多相关文章

  1. hdu2612 Find a way BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 思路: 裸的BFS,对于Y,M分别进行BFS,求出其分别到达各个点的最小时间: 然后对于@的点, ...

  2. hdu2612(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意:求2个点到任意一个KFC的距离之和,使其最小. 分析:由两个点出发分别两次bfs,求得到每 ...

  3. 简单bfs(hdu2612)

    #include<stdio.h>#include<string.h>#include<queue>#define INF 0x3f3f3f3fusing name ...

  4. HDU-2612.Find way .(不同起点不同终点的BFS)

    我要被这个好用的memset气死了...... 真香 #include <cstring> #include <string> int main () { ]; memset( ...

  5. HDU2612 BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 , 一道比较简单的广搜(BFS)题目. 算法: 设置两个dist[][]数组,记录Y和M到几个K ...

  6. HDU2612 Find a way (跑两遍BFS)

    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...

  7. hdu 2612 Find a way(BFS)

    题目链接:hdu2612 思路:题意是求两个人到某一个KFC花费时间和最小,其实就是求最短距离和,用两个BFS,分别以两个人为起点,分别记录下两人到每个KFC的距离,然后求出最小的和 #include ...

  8. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  9. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

随机推荐

  1. 动手实践 Linux VLAN

    本节我们来看如何在实验环境中实施和配置如下 VLAN 网络 配置 VLAN 编辑 /etc/network/interfaces,配置 eth0.10.brvlan10.eth0.20 和 brvla ...

  2. Yii 之Session使用

    public function actionIndex(){ $session = \YII::$app->session; //判断session是否开启 if(!$session->i ...

  3. HDU 6149 Valley Numer II(状压DP)

    题目链接 HDU6149 百度之星复赛的题目……比赛的时候并没有做出来. 由于低点只有15个,所以我们可以考虑状压DP. 利用01背包的思想,依次考虑每个低点,然后枚举每个状态. 在每个状态里面任意枚 ...

  4. AIO

    IBM® 市场 (英文) 提交   我的 IBM 站点导航   学习 开发 社区 学习 Java technology 内容   概览 简单介绍 Asynchronous I/O 开始简单的异步 I/ ...

  5. #ifdef #endif #if #endif

    c语言里所有以#开头的都是预编译指令,就是在正式编译之前,让编译器做一些预处理的工作. #ifdef DEBUG printf("variable x has value = %d\n&qu ...

  6. 深入GCD(三): Dispatch Sources

    何为Dispatch Sources简单来说,dispatch source是一个监视某些类型事件的对象.当这些事件发生时,它自动将一个block放入一个dispatch queue的执行例程中.说的 ...

  7. 【mac】屏幕截图快捷键

    这里只说四种 1.command+shift+4 截图是区域截图,会自动保存在桌面上 2.command+shift+control+4 区域截图,截图会自动保存在剪切板中,然后你可以通过comman ...

  8. 【IntelliJ Idea】启动参数JVM参数的配置 优先级高于 application.yaml/application.properties中的配置,前者可以覆盖后者的配置

  9. centos 7 安装官方LAMP(Apache+PHP5+MySQL)

    启用Apache2 yum install httpd systemctl start httpd.service systemctl status httpd systemctl enable ht ...

  10. npm WARN uninstall not installed in /Users/hrt0kmt/node_modules: "xxx"

    You may meet this error on home directory. % npm uninstall appium npm WARN uninstall not installed i ...