HDU - 2612 Find a way 【BFS】
题目链接
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;
}
}
HDU - 2612 Find a way 【BFS】的更多相关文章
- HDU 1548 A strange lift【BFS】
题意:给出一个电梯,给出它的层数f,给出起点s,终点g,以及在每一层能够上或者下w[i]层,问至少需要按多少次按钮到达终点. 和POJ catch that cow一样,直接用了那一题的代码,发现一直 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- HDU 2612 Find a way【多起点多终点BFS/两次BFS】
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submis ...
- HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)
Friends and Enemies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 5969 最大的位或 【贪心】 (2016年中国大学生程序设计竞赛(合肥))
最大的位或 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem De ...
- HDU 5968 异或密码 【模拟】 2016年中国大学生程序设计竞赛(合肥)
异或密码 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Des ...
- HDU 5949 Relative atomic mass 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
Relative atomic mass Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
随机推荐
- 传输层:TCP 协议
传输层:TCP 协议 一.概述 TCP 和 UDP 处在同一层——运输层,但是它们有很多的不同.TCP 是 TCP/IP 系列协议中最复杂的部分,它具有以下特点: (1) TCP 提供 可靠的 数据传 ...
- mybatis配置mapperLocations多个路径
<property name="mapperLocations"> <array> <value>classpath*:/mybatis-con ...
- openTK学习
简介 the Open Tool Kit (OpenTK), 是对 OpenGL.OpenAL.OpenCL 的跨平台的封装,使用 C# 编写,它可以用在Mono.dotNet的语言:c#.VB.C+ ...
- ElasticSearch和Hive做整合
1.上传elasticsearh-hadoop的jar包到server1-hadoop-namenode-01上 在server1-hadoop-namenode-01上执行: cp /home/d ...
- JAVA基础针对自己薄弱环节总结01(循环之前的知识)
java中的标识符 组成:数字.字母.下划线.美元$符号组成. 规则:不能由数字开头. 类名:每一个单词的首字母大写 包名:所有小写 变量名.方法名:第一个单词首字母小写.后面首字母大写 常 ...
- element的el-tabs控制,以及el-select 多选默认值
一.el-tabs 1.element自己已经封装好了,当切换时v-model的值自动切换为el-tabs-pane的name对应的值. 如下: <el-tabs v-model='active ...
- vue 中 this.$router.push() 路由跳转传参 及 参数接收的方法
传递参数的方法:1.Params 由于动态路由也是传递params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否则params将无效.需要用name ...
- 移动端去掉按钮button默认样式
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- struts2实现文件查看、下载
CreateTime--2017年9月7日10:25:33 Author:Marydon struts2实现文件查看.下载 1.界面展示 <a style="color: #199 ...
- C# DataTable 总结
(1)构造函数 DataTable() 不带参数初始化DataTable 类的新实例. DataTable(string tableName) 用指定的表名初始化DataTable 类的新实例. ...