题目连接

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的更多相关文章

  1. HDU 2612 Find a way(找条路)

    HDU 2612 Find a way(找条路) 00 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)   Problem  ...

  2. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  3. BFS(最短路) HDU 2612 Find a way

    题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...

  4. 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 ...

  5. HDU 2612 - Find a way - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...

  6. HDU 2612 Find a way bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...

  7. (广搜) Find a way -- hdu -- 2612

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others) ...

  8. HDU - 2612 Find a way 【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意 有两个人 要去一个城市中的KFC 一个城市中有多个KFC 求两个人到哪一个KFC的总时间最 ...

  9. HDU 2612 (BFS搜索+多终点)

    题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...

随机推荐

  1. ODCA最佳实践翻译:Architecting Cloud-Aware Applications (一)

    Architecting Cloud-Aware Applications ** ODCA(Open Data Center Alliance)最佳实践 ** MagicBowen(e.bowen.w ...

  2. [前端 4] 使用Js实现图片上传预览

    导读:今天做图片上传预览,刚开始的做法是,先将图片上传到Nginx,然后重新加载页面才能看到这个图片.在这个过程中,用户一直都看不到自己上传的文件是什么样子.Ps:我发现我真的有强迫症了,都告诉我说不 ...

  3. CAPI HTTP服务搭建(文件在本机)

    南康HTTP服务器搭建步骤如下: 1.环境准备 2.添加应用程序 3.访问测试 4.添加虚拟目录 5.下载测试 6.上传测试 7.最终效果 8.权限设置 有问题请留言.

  4. 在Qt中使用AnyCAD三维建模控件

    AnyCAD C++ SDK专为Qt框架增加了AnyPlatformQt.lib模块,在Qt中使用AnyCAD三维建模控件变得十分简单. 下载 Qt高速下载:http://pan.baidu.com/ ...

  5. url中文参数解决方案

    首先,弄清楚为什么url传递中文会转码或者乱码,以及http头 contentType="text/html; charset=GBK" 的作用. html代码会经过web服务器, ...

  6. .NET Web开发总结

    在aspx文件中  创建控件 在右下角有控件信息 按类排序 会将控件信息安装类排序 点击控件 会增加属性页面的分页[事件]页面  可以增加其事件函数 字符串操作及其时间操作 fn_name.Inser ...

  7. NSBundle UIImageView &UIButton

    1.NSBundle 1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹 2> 利用mainBundle就可以访问软件资源包中的任何资源 3> 模拟器应 ...

  8. 一些peoplecode小技巧平【二】

    1. Set component changed page field property: For understanding this open a page in application desi ...

  9. 打印日志 Log

    Log.v(tag,msg);所有内容 Log.d(tag,msg);debug Log.i(tag,msg);一般信息 Log.w(tag,msg);警告信息 Log.e(tag,msg);错误信息

  10. public void onItemClick(AdapterView arg0, View view, int position,long arg3)详解【整理自网络】

    参考自: http://blog.csdn.net/zwq1457/article/details/8282717 http://blog.iamzsx.me/show.html?id=147001 ...