hdu 2612 Find a way
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=2612
Find a way
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 \leq n,m \leq 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
两次bfs,注意若有一方无法到达某个kfc,这个点就不能取。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::min;
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::map;
using std::pair;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
const int INF = ~0u >> ;
typedef unsigned long long ull;
char G[N][N];
int H, W, Sx, Sy, Dx, Dy, dist[][N][N];
const int dx[] = { , , -, }, dy[] = { -, , , };
struct Node {
int x, y;
Node(int i = , int j = ) :x(i), y(j) {}
}Y, M;
void bfs(int x, int y, bool d) {
queue<Node> q;
q.push(Node(x, y));
dist[d][x][y] = ;
while (!q.empty()) {
Node t = q.front(); q.pop();
rep(i, ) {
int nx = t.x + dx[i], ny = t.y + dy[i];
if (nx < || nx >= H || ny < || ny >= W) continue;
if (G[nx][ny] == '#' || dist[d][nx][ny]) continue;
dist[d][nx][ny] = dist[d][t.x][t.y] + ;
q.push(Node(nx, ny));
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
while (~scanf("%d %d", &H, &W)) {
cls(dist, );
vector<Node> kfc;
rep(i, H) {
scanf("%s", G[i]);
rep(j, W) {
if (G[i][j] == 'Y') Sx = i, Sy = j;
if (G[i][j] == 'M') Dx = i, Dy = j;
if (G[i][j] == '@') kfc.pb(Node(i, j));
}
}
bfs(Sx, Sy, ), bfs(Dx, Dy, );
int res = INF;
rep(i, sz(kfc)) {
Node &k = kfc[i];
int A = dist[][k.x][k.y], B = dist[][k.x][k.y];
if (!A || !B) continue;
res = min(res, A + B);
}
printf("%d\n", res * );
}
return ;
}
hdu 2612 Find a way的更多相关文章
- HDU 2612 Find a way(找条路)
HDU 2612 Find a way(找条路) 00 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- BFS(最短路) HDU 2612 Find a way
题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...
- HDU 2612 Find a way(双向bfs)
题目代号:HDU 2612 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 M ...
- HDU 2612 - Find a way - [BFS]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...
- HDU 2612 Find a way bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...
- (广搜) Find a way -- hdu -- 2612
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others) ...
- HDU - 2612 Find a way 【BFS】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意 有两个人 要去一个城市中的KFC 一个城市中有多个KFC 求两个人到哪一个KFC的总时间最 ...
- HDU 2612 (BFS搜索+多终点)
题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...
随机推荐
- 请添加 MIME 映射
HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 以管理员运行命令:C:\Wind ...
- CODEVS 1001 舒适的路线
思路:先按照速度大小对边排序,再枚举最终路径中的速度最大值,并查集,更新答案 #include<iostream> #include<vector> #include<a ...
- StackExchange.Redis使用和封装小试
StackExchange.Redis使用和封装小试 https://git.oschina.net/Ultralisk/ThorActor/blob/ThorActor/DBUtility/Redi ...
- 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
摘 自: http://blog.csdn.net/fxhflower/article/details/7276820 可使用以下代码: //Create our own namespaces for ...
- hdu 2255 奔小康赚大钱 KM算法
看到这么奇葩的题目名我笑了,后来这么一个裸的KM调了2小时我哭了…… 这是个裸的KM算法,也没什么多说的,主要是注意多组数据时,每次都要把各种数组清空啊,赋值啊什么的,反正比较麻烦.至于为什么调了2小 ...
- Large-Scale Named Entity Disambiguation Based on Wikipedia Data
Large-Scale Named Entity Disambiguation Based on Wikipedia Data 基于维基百科数据的大规模命名实体消岐 1.引言 1.1. 概念 实体(e ...
- Android异常一、异步任务导致的窗口句柄泄漏问题(转)
05-05 10:36:41.009: E/WindowManager(4243): Activity com.tao.MyActivity has leaked window com.android ...
- 发送Ajax请求获取JSON格式数据
Aspx前端页面: <script type="text/javascript"> $(function () { $.getJSON("Ajax/TestA ...
- sqoop导入数据到hive
1.1hive-import参数 使用--hive-import就可以将数据导入到hive中,但是下面这个命令执行后会报错,报错信息如下: sqoop import --connect jdbc:my ...
- HTML5高防win8风格
使用HTML5做了一个高防Win8的页面,大家看看怎么样 Java..NET.PHP.Android.iOS.HTML5,CSS3. web前端.视频资料下载网站 http://51pansou.c ...