题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=2612

题意

有两个人 要去一个城市中的KFC 一个城市中有多个KFC 求两个人到哪一个KFC的总时间最少 求出这个最少的总时间

思路

用BFS打一遍表 求出两个人每人到 每个 KFC 的时间 然后 遍历一下 更新答案

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1);
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 5;
const int MOD = 1e9 + 7; int Move[][2]
{
-1, 0, // up
1, 0, // down
0,-1, // left
0, 1, // right
}; struct Node
{
int x, y, step;
}tmp; string G[205]; int v[205][205];
int dis[205][205][2]; int n, m; int sx[2], sy[2]; queue <Node> q; bool ok(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= m || G[x][y] == '#' || v[x][y] == 1)
return false;
return true;
} void bfs(int vis)
{
while (!q.empty())
{
int x = q.front().x;
int y = q.front().y;
int step = q.front().step;
q.pop();
if (G[x][y] == '@')
dis[x][y][vis] = min(dis[x][y][vis], step);
for (int i = 0; i < 4; i++)
{
tmp.x = x + Move[i][0];
tmp.y = y + Move[i][1];
tmp.step = step + 1;
if (ok(tmp.x, tmp.y))
{
q.push(tmp);
v[tmp.x][tmp.y] = 1;
}
}
}
} int main()
{
while (~scanf("%d%d", &n, &m))
{
for (int i = 0; i < n; i++)
{
cin >> G[i];
for (int j = 0; j < m; j++)
{
if (G[i][j] == 'Y')
{
sx[0] = i;
sy[0] = j;
}
else if (G[i][j] == 'M')
{
sx[1] = i;
sy[1] = j;
}
}
}
memset(dis, 0x3f, sizeof(dis));
for (int i = 0; i < 2; i++)
{
CLR(v);
tmp.x = sx[i];
tmp.y = sy[i];
v[tmp.x][tmp.y] = 1;
tmp.step = 0;
while (!q.empty())
q.pop();
q.push(tmp);
bfs(i);
}
int ans = INF;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (G[i][j] == '@')
ans = min(ans, dis[i][j][0] + dis[i][j][1]);
}
}
cout << ans * 11 << endl;
}
}

最新文章

  1. Python JPype 在 Win7 下安装与使用
  2. java基础 绘图技术.坦克大战 之java绘图坐标体系(二)
  3. CS193P - 2016年秋 第三讲 Swift 语言及 Foundation 框架
  4. iOS开发之CocoaPods的使用
  5. OD使用教程11
  6. [源码]NumberToUpper 数字转中文
  7. jQuery插件:跨浏览器复制jQuery-zclip
  8. JS-鼠标经过显示二级菜单
  9. C#压缩图片1
  10. Linux 虚拟机和物理机配互信出现无法连接
  11. 11g RAC R2 日常巡检--Grid
  12. win8安装matlab7.0
  13. &gt;炫酷的计时器效果Canvas绘图与动画&lt;
  14. php使用正则
  15. SnackbarUtils:一行代码搞定Snackbar
  16. UVA 1400 线段树
  17. #图# #SPFA# ----- codevs1021 玛丽卡
  18. Apache 和 Tomcat联系和区别
  19. 在windows下Apache安装配置
  20. 框架MyBatis

热门文章

  1. EasyMvc入门教程-基本控件说明(2)定时器
  2. 简单便捷的纯PHP网盘程序 Veno File Manager 2.6.3(VFM2)
  3. Struts2学习记录-Value Stack(值栈)和OGNL表达式
  4. Mogodb集群搭建
  5. HTML5-SQLLite连接
  6. linux链接外网手动设置
  7. 关于layui-layer独立组件--弹出层
  8. js玩转数字----取整,四舍五入,数字字符串转换
  9. Leetcode Array 4 Median of Two Sorted Arrays
  10. 李振杰:火狐Mozilla被黑事件的启发