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相对路径转绝对路径,绝对路径转相对路径
1.绝对路径转相对路径 print os.path.relpath("d:/MyProj/MyFile.txt") #..\MyProj\MyFile.txt 是根据当前路径的相对 ...
- Python中通过cx_Oracle访问数据库遇到的问题总结
以下是Python中通过cx_Oracle操作数据库的过程中我所遇到的问题总结,感谢我们测试组的前辈朱勃给予的帮助最终解决了下列两个问题: 1)安装cx_Oracle会遇到的问题:在Windo ...
- C#中八皇后问题的递归解法——N皇后
百度测试部2015年10月份的面试题之——八皇后. 八皇后问题的介绍在此.以下是用递归思想实现八皇后-N皇后. 代码如下: using System;using System.Collections. ...
- jquery简单插件到复杂插件(1)--tabs
写在前面,到了新公司开始转做前段,之前一直写php,一共写了半年,转过来,jq都用不好,但是还是得不断的学习,谁没菜过.从最简单的开始写,最近也在学习些html5的小游戏,加油吧.js原生写的可以说惨 ...
- Bootstrap——Jumbotron编写
<div class="jumbotron"> <h1>Navbar example</h1> <p>T ...
- FlashFXP命令行
flashfxp.exe -upload ftp://user:pass@ip:port -localpath="本地路径" -remotepath="远程FTP上的路 ...
- Objective-C学习笔记_Xcode模拟命令行填入参数执行
菜单Product->Edit Scheme 左边找到run xxx,点击后再邮编选择Arguments面板中就可以设置Xcode在运行命令行app时模拟输入参数. 设置完成后再次run就会自动 ...
- Java动态代理 cglib
代理模式:为某些对象提供代理以实现对这个对象的访问. 对一个对象进行访问控制的原因是为了只有在我们确实需要这个对象时才对它进行创建和初始化. 一般包括以下组件: 被代理者接口:提供被代理者的访问途径. ...
- GMT时间转换
/// <summary> /// GMT时间转成本地时间 /// </summary> /// <param name="gmt">字符串形式 ...
- DrawIndexedPrimitive参数详解
HRESULT DrawIndexedPrimitive( [in] D3DPRIMITIVETYPE Type, [in] INT BaseVertexIndex, [in] UINT Min ...