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目标必须一致. 于 ...
随机推荐
- 常用上传shell脚本
#!/bin/bash FTP_IP=127.0.0.1 FTP_USER="aaa" FTP_PSW="bbb" GAME_NAME="ccc&qu ...
- number对象,bom对象
number对象 新创建一个number的对象,toFixed是精确到位数 var num =new Number('123.1231'); console.log(num.toFixed(1)); ...
- excel中单元格计算
首先,得明确excel中相对引用和绝对引用的概念,这里$符号起着关键作用,当在一个行或列的指示符前面加$则表示绝对引用,否则相对引用,具体: 1.相对引用,复制公式时地址跟着发生变化,如C1单元格有公 ...
- note name
谦谦君子:借用<周易·谦>:“初六,谦谦君子,用涉大川,吉.” 温润如玉:化用<诗经·卫风·淇奥>“有匪君子,如切如磋,如琢如磨”之义.
- 洛谷P1470 最长前缀 Longest Prefix
P1470 最长前缀 Longest Prefix 73通过 236提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 求大神指导,为何错? 题目描述 在生 ...
- windows异常调用顺序
(一) 发生异常时系统的处理顺序(by Jeremy Gordon, Hume): 1.系统首先判断异常是否应发送给目标程序的异常处理例程,如果决定应该发送,并且目标程序正在被调试,则系统 挂 ...
- 问 如何使用css将select的边框以及右边的小三角形去掉?
最好css2,css3都给出解决方案,效果如下: CSS2 只能使用div和ul进行模拟了,结构很简单,具体可参考Alice的 button-dropdownCSS3 可以使用CSS3的属性appea ...
- C++ 牛人博客(不断更新中...)
http://www.zhangjiee.com/ 新浪微博@独酌逸醉. Github. GitCafe. stackoverflow. Quora http://cpp1x.org/ 刘未鹏 | M ...
- iPhone 6 (iOS 9.2) extractiion failed by XRY
My colleague extracted an iPhone 6 with XRY and it is iOS 9.2 . Unfortunately the Wizard crashed and ...
- CSS3新增伪类
p:last-of-type 选择其父元素的最后的一个P元素 p:last-child 选择其父元素的最后子元素(一定是P才行) p:first-of-type ...