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 ...
随机推荐
- 200多种Android动画效果的强悍框架
admin 发布于2015-10-23 14:33 363/68015 [精品推荐]200多种Android动画效果的强悍框架,太全了,不看这个,再有动画的问题,不理你了^@^ 功能模块和技术方案 只 ...
- 队列容易queue
/*先进先出*/#include<iostream>#include<queue> //使用队列必须包含头文件using namespace std;int main(){ q ...
- ylbtech-czgfh(规范化)-数据库设计
ylbtech-DatabaseDesgin:ylbtech-czgfh(规范化)-数据库设计 DatabaseName:czgfh(财政规范化) Model:账户模块.系统时间设计模块.上报自评和审 ...
- ylb:SQL 系统函数
ylbtech-SQL Server: SQL Server-SQL 系统函数 SQL 系统函数 1,ylb:SQL 系统函数 返回顶部 -- ============================ ...
- 【Salvation】——怪物角色动画&主角碰撞死亡动画
写在前面:这个动画功能同样也是使用JavaScript编写脚本,在Unity3D游戏引擎的环境中实现,在怪物的角色动画中,很多与人物相同,这里不再重复. 一.设计敌人 拖一个精英sprite到层次面板 ...
- JDK8 下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
JDK8 下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 安装时最好在 ...
- 服务器,数据库连接注意mysql的user表
update user set host='localhost' where user='root';
- hibernate 过滤
1.可以使用@Where的过滤,不过这种过滤式全局的,支持延迟加载. 2.可以使用@Filters,这种过滤不支持延迟加载. @Entity@Table(name = "qc315_tous ...
- poj 2479 Maximum sum(递推)
题意:给定n个数,求两段连续不重叠子段的最大和. 思路非常easy.把原串划为两段.求两段的连续最大子串和之和,这里要先预处理一下,用lmax数组表示1到i的最大连续子串和,用rmax数组表示n ...
- HDU1323_Perfection【水题】
Perfection Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...