hiho1092_have lunch together
题目
两个人从同一个点出发,在一个餐厅中寻找两个相邻的座位,需要是的从出发点到达座位的距离总和最短。题目链接: Have Lunch Together
最短路程,一开始以为要用dijkstra等图算法,发现完全不用,直接用BFS进行搜索,并标记到达每个点的最短距离。一次BFS求出从起始点
到达所有点的最短距离之后,再遍历每个点,判断该点是否是合法的座位(能够从起始点到达,且为座位),然后对每个座位,看它四周的点是否是合法的座位,然
后求出两个合法的座位的最短距离和的最小值。
实现
#include<stdio.h>
#include<cmath>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<deque>
#include<string>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define min(a, b) (a) < (b)? (a) : (b)
char gMap[105][105];
int gMinDist[105][105];
int gMoveStep[4][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
struct Node {
int row;
int col;
int step;
Node(int r, int c, int s) :
row(r), col(c), step(s) {};
};
//BFS 求出从出发点到达每个能够到达的点的最短距离
void Bfs(int start_row, int start_col) {
queue<Node> Q;
Node node(start_row, start_col, 0);
Q.push(node);
gMinDist[start_row][start_col] = 0;
while (!Q.empty()) {
node = Q.front();
Q.pop();
for (int i = 0; i < 4; i++) {
int next_row = node.row + gMoveStep[i][0];
int next_col = node.col + gMoveStep[i][1];
if ((gMap[next_row][next_col] == 'S' || gMap[next_row][next_col] == '.') && gMinDist[next_row][next_col] == -1) {
gMinDist[next_row][next_col] = node.step + 1;
if(gMap[next_row][next_col] == '.')
Q.push({ next_row, next_col, node.step + 1 });
}
}
}
}
int MinDist(int n, int m) {
int min_dist = 1 << 29;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (gMap[i][j] == 'S' && gMinDist[i][j] != -1) { // 为一个座位,且在BFS过程中能够到达
for (int k = 0; k < 4; k++) {
int ii = i + gMoveStep[k][0];
int jj = j + gMoveStep[k][1];
// 为一个座位,且在BFS过程中能够到达
if (ii >= 1 && ii <= n && jj >= 1 && jj <= m && gMap[ii][jj] == 'S' && gMinDist[ii][jj] != -1) {
min_dist = min(min_dist, gMinDist[i][j] + gMinDist[ii][jj]);
}
}
}
}
}
return min_dist;
}
int main() {
int n, m, start_row, start_col;
scanf("%d %d", &n, &m);
memset(gMap, '#', sizeof(gMap));
memset(gMinDist, -1, sizeof(gMinDist));
for (int i = 1; i <= n; i++) {
getchar();
for (int j = 1; j <= m; j++) {
scanf("%c", &gMap[i][j]);
if (gMap[i][j] == 'H') {
start_row = i;
start_col = j;
}
}
}
Bfs(start_row, start_col);
int min_dist = MinDist(n, m);
if (min_dist == (1 << 29)) {
printf("Hi and Ho will not have lunch.\n");
}else
printf("%d\n", min_dist);
return 0;
}
hiho1092_have lunch together的更多相关文章
- HDU4807 Lunch Time(费用流变种)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4807 Description The campus of Nanjing Universit ...
- 水题 ZOJ 3875 Lunch Time
题目传送门 /* 水题:找排序找中间的价格,若有两个,选价格大的: 写的是有点搓:) */ #include <cstdio> #include <iostream> #inc ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...
- 第十二届浙江省大学生程序设计大赛-Lunch Time 分类: 比赛 2015-06-26 14:30 5人阅读 评论(0) 收藏
Lunch Time Time Limit: 2 Seconds Memory Limit: 65536 KB The 999th Zhejiang Provincial Collegiate Pro ...
- Codeforces Gym 100637B B. Lunch 找规律
B. Lunch Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/B Des ...
- build/envsetup.sh内lunch解析
........ # 测试device是否存在且是一个目录 并且 只查找device目录4层以上的子目录,名字为vendorsetup.sh 并且 将命令执行的错误报告直接送往回收站 不显示在屏幕上 ...
- hihoCoder 1092 : Have Lunch Together
题目大意:小hi和小ho去咖啡厅喝咖啡,咖啡厅可以看作是n * m的矩阵,每个点要么为空,要么被人.障碍物.椅子所占据,小hi和小ho想要找两个相邻的椅子.起初两个人都在同一个点,求两人到达满足要求的 ...
- Lunch War with the Donkey CSU - 2084
Jingze is a big figure in California State University for his stubbornness. Because of his new failu ...
- 每日英语:Making the Most of Your Lunch Hour
More Americans are eating lunch at their desks or even forgoing it altogether. Is passing up a prope ...
随机推荐
- Python科学计算环境推荐——Anaconda
最近在用Python做中文自然语言处理.使用的IDE是PyCharm.PyCharm确实是Python开发之首选,但用于科学计算方面,还略有欠缺.为此我尝试过Enthought Canopy,但Can ...
- 制作变形、移位、扭曲等效果:《CSS3 transform》
今天开始我们一起来学习有关于CSS3制作动画的几个属性:变形(transform).转换(transition)和动画(animation)等更高级的CSS3技术.本文主要介绍的是这三个属性之中的第一 ...
- phpcms 03
继续上次对header.html文件的分析 logo<a href="{siteurl($siteid)}/"><img src="{IMG_PATH} ...
- 深入javascript作用域链到闭包
我之前用过闭包,用过this,虽然很多时候知道是这么一回事,但是确实理解上还不够深入.再一次看javascript高级程序设计这本书时,发现一起很多疑难问题竟然都懂了,所以总结一下一些理解,难免有错, ...
- Dungeon Master bfs
time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...
- Java_JDK_HashMap
(二)HashMap 需要注意的无非几点: 是什么结构,如何存储的? 如何加入元素?既然是hashMap,那么是如何计算hashcode的呢?遇到冲突又是如何解决的呢? 如何删除元素? 当容量不够时是 ...
- Reading Csv Files with Text_io in Oracle D2k Forms
Below is the example to read and import comma delimited csv file in oracle forms with D2k_Delimited_ ...
- ABAP Enhancement:第一部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- VS连接Windows Mobile模拟器
原文:http://www.cnblogs.com/xjimmyshcn/archive/2011/07/19/2111087.html 一.WinCE 模拟器通过ActiveSync 6.1(即Wi ...
- Nemerle Quick Guide
This is a quick guide covering nearly all of Nemerle's features. It should be especially useful to a ...