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 ...
随机推荐
- Unity-Animator深入系列---状态机面板深入
回到 Animator深入系列总目录 本篇不讲解所有的面板功能,只是针对一些非常用功能进行介绍. 1.状态 1.1状态简介 简单的不做介绍了,需要特别注意: 1.Paramter勾选后可以指定参数控制 ...
- Poj(3686),最小权匹配,多重匹配,KM
题目链接 The Windy's | Time Limit: 5000MS | Memory Limit: 65536K | | Total Submissions: 4939 | Accepted: ...
- Combox控件初始化项的技巧
有时候combox下拉项就是几条固定的,不需要做数据绑定,但是如果直接采用字符串集合编辑器的时候,SelectedValue貌似会不起作用. 于是简单的封装一下 /// <summary> ...
- GROUP BY和ORDER BY共用
SELECT BatchNumber,MAX(Id) FROM dbo.SceneryOrder AND BatchNumber<>'' GROUP BY BatchNumber DESC
- jquery easyui datagrid使用参考
jquery easyui datagrid使用参考 创建datagrid 在页面上添加一个div或table标签,然后用jquery获取这个标签,并初始化一个datagrid.代码如下: 页面上 ...
- java获取指定时间的年月日
作者:Night Silent链接:http://www.zhihu.com/question/35650798/answer/63983440来源:知乎著作权归作者所有,转载请联系作者获得授权.1. ...
- SpringMVC拦截器2(资源和权限管理)(作为补充说明)
SpringMVC拦截器(资源和权限管理) 1.DispatcherServlet SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServle ...
- Centos 中如何快速定制二进制的内核RPM 包
随着Linux服务器越来越多了,底层系统内核想要保持版本统一就需要定制专门的二进制安装包来便捷的升级和管理. RedHat系那当然就是使用rpmbuild来做定制化管理了. 今天我们分俩个部分(roo ...
- CentOS 6.5中linux grub修复
在使用Linux的过程中,难免会出现开机提示grub >而无法启动,可能是系统中/boot/grub文件丢失等原因造成的,当出现此问题的时候只要系统分区没有格式化一般是可以修复的,下面就以虚拟 ...
- 利用JDBC处理mysql大数据--大文本和二进制文件等
转载自http://www.cnblogs.com/xdp-gacl/p/3982581.html 一.基本概念 大数据也称之为LOB(Large Objects),LOB又分为:clob和blob, ...