题目链接: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. Infinite monkey theorem(hdu 3689)

    题意:问随机生成一个长度为m(m<=1000)长度的字符串,出现某个子串s的概率是多少. /* KMP+DP 设f[i][j]表示A生成到第i位,此时B串匹配到第j位的概率. 转移方程为f[i+ ...

  2. LoadBitmap(IDB_BITMAP1) -- 未定义标识符 IDB_BITMAP1

    错误原因:1:头文件没有加入 #include"resource.h" 2:没有导入该资源或者资源ID错误

  3. 【Tomcat】解决Tomcat catalina.out 不断成长导致档案过大的问题

    Tomcat的网站上的说法http://wiki.apache.org/tomcat/FAQ/Logging#Q6: System.out 和 System.err 都被打印到 catalina.ou ...

  4. android 禁止ViewPager滑动

    最近项目中,有个需求就是要禁止ViewPager滑动事件,我们看下360手机助手的界面,风格就类似这样的 大家如果使用过360手机助手就会发现中间内容是不可以滑动的,现在写一个demo,讲下怎么禁止V ...

  5. 有关WebView开发问题(转)

    http://blog.sina.com.cn/s/blog_8241e8510101btvk.html 如何创建WebView: 1.添加权限:AndroidManifest.xml中必须使用许可& ...

  6. 从Activity的启动流程理解Binder

    简述 关于Activity启动流程和Binder的文章很多,大多数是分开来讲的,本文将二者结合起来,着重分析启动流程中跨进程方面的细节,其实,启动流程看似调用繁多,主要是复杂在Activity栈管理等 ...

  7. ZOJ - 4020 Traffic Light (BFS)

    [传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020 [题目大意]从起点(sx, sy)出发,要到达(ex , ...

  8. Access restriction: The method 'CharacterEncoder.encode(byte[])' is not API...

    问题描述:Access restriction: The method 'CharacterEncoder.encode(byte[])' is not API... 解决方法:这种错误是eclips ...

  9. java list实现树形结构

    1.javabean import java.util.List; public class TreeNode { private String id; private String parentId ...

  10. Centos 6.x 安装Python 3.4.3

    [root@squid ~]# sed -i 's#keepcache=0#keepcache=1#g' /etc/yum.conf [root@squid ~]# grep keepcache /e ...