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 ...
随机推荐
- n条直线最多能将一个平面分成多少部分?
f(n)=n(n+1)/2+1 原理:第N条直线可以被前N-1条直线分为N段,对于 每1段则将平面分为两份,所以对于前 f(n)=f(n-1)+n. f(n-1)=f(n-2)+n-1 ...... ...
- python学习笔记-day4笔记 常用内置函数与装饰器
1.常用的python函数 abs 求绝对值 all 判断迭代器中所有的数据是否为真或者可迭代数据为空,返回真,否则返回假 any ...
- Entity Framework 第一篇
这段时间研究了orm框架EF 写一写研究的历程和心得 先贴上核心代码 public interface ITransaction { bool IsTransaction { get;} void B ...
- 关于python的__name__理解
Python中,每个模块有个__name__属性,当模块是在自己文件下执行的,那么它的__name__属性是__main__,而当它被引入到别的模块中,那么在别的模块下(import模块名 可以引入一 ...
- HDU2112 HDU Today 最短路+字符串哈希
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Python3基础 使用技巧:多行同时缩进
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- 【转载】nedmalloc结构分析
原文:nedmalloc结构分析 nedmalloc是一个跨平台的高性能多线程内存分配库,很多库都使用它,例如:OGRE.现在我们来看看nedmalloc的实现 (以WIN32部分为例) 位操作 ...
- MySQL复制表结构表数据
MySQL复制表结构 表数据 1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete ...
- 【转】利用Pspice分析放大器环路的稳定性
文章来源: http://www.21ic.com/app/test/201108/90808.htm 虽然在较低频率下可以较轻松地检查一个简单放大器的稳定性,但评估一个较为复杂的电路是否稳定,难度可 ...
- [SAP ABAP开发技术总结]采购、销售、生产简单业务流程
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...