题目链接: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. Codeforces Round #267 (Div. 2) C. George and Job (dp)

    wa哭了,,t哭了,,还是看了题解... 8170436                 2014-10-11 06:41:51     njczy2010     C - George and Jo ...

  2. Python入门--14--字典

    基础知识: 0.python对值没有要求,但对key有些要求,必须是非变量(变量,列表,字典) 1.在列表中使用成员资格操作符,和在字典中使用成员资格操作符,后者会更快 2.fromkeys这个函数会 ...

  3. html--添加、删除滚动条

    1:若想给div添加滚动条: style="overflow-y:scroll";(添加纵向滚动条) style="overflow-x:scroll";(添加 ...

  4. Day 3 网络基础

    网络基础 一.什么是互联网协议及为何要有互联网协议 ? 互联网协议:指的就是一系列统一的标准,这些标准称之为互联网协议.互联网的本质就是一系列的协议,总称为‘互联网协议’(Internet Proto ...

  5. NOIP临考经验(转)

    [COGS]NOIP临考经验 1.  提前15分钟入场,此时静坐调整心态,适当的深呼吸 2.  打开编辑器并调整为自己喜欢的界面 3.  熟悉文件目录,写好准确无误的代码模板 4.  压缩包或许还不能 ...

  6. Nginx常用命令(启动/重启/停止/测试配置文件/重新加载配置文件)

    Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的. Nginx 的参数包括有如下几个: 使用: /usr/local/ngin ...

  7. 转:NetBeans的远程Linux C开发实践

    转: http://blog.csdn.net/jacktan/article/details/9268535 一直以来总觉得NetBeans生活在Eclipse的阴影下,同样做为一款不错的基于Jav ...

  8. 天津政府应急系统之GIS一张图(arcgis api for flex)解说(二)鹰眼模块

    解说GIS功能模块实现之前,先大概说一下flexviewer的核心配置文件config.xml,系统额GIS功能widget菜单布局.系统的样式.地图资源等等都是在这里配置的,这里对flexviewe ...

  9. pomelo加入定时任务

    需求:在arenaserver下添加一个rank定时任务,每一分钟对对玩家进行一次排行. 首先在game-server/app/servers/arena文件夹下添加cron文件夹. 在game-se ...

  10. node 爬虫 --- 将爬取到的数据,保存到 mysql 数据库中

    步骤一:安装必要模块 (1)cheerio模块 ,一个类似jQuery的选择器模块,分析HTML利器. (2)request模块,让http请求变的更加简单 (3)mysql模块,node连接mysq ...