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 ...
随机推荐
- C#微信开发文档
C#微信开发文档 开发前准备 微信公众平台链接: https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN 开发初期我们使用测 ...
- spring Aop中aop:advisor 与 aop:aspect的区别
转载:http://blog.csdn.net/u011710466/article/details/52888277 在spring的配置中,会用到这两个标签.那么他们的区别是什么呢? ...
- Python中通过cx_Oracle访问数据库遇到的问题总结
以下是Python中通过cx_Oracle操作数据库的过程中我所遇到的问题总结,感谢我们测试组的前辈朱勃给予的帮助最终解决了下列两个问题: 1)安装cx_Oracle会遇到的问题:在Windo ...
- 2015 AlBaath Collegiate Programming Contest A
Description Tamer is traveling with his brother on a long highway. He sees a traffic light at a dist ...
- c++のdll两种调用方式
调用DLL有两种方法:静态调用和动态调用. (一).静态调用其步骤如下: 1.把你的youApp.DLL拷到你目标工程(需调用youApp.DLL的工程)的Debug目录下; 2.把你的youApp. ...
- datagrid后台分页js.js
$(function () { gridbind(); bindData(); }); //表格绑定function gridbind() { $('#dg').datagrid({ title: ' ...
- php访问mysql数据库
//建一个连接,造一个连接对象 $db = new MySQLi("localhost","root","123","mydb&q ...
- SqlSever基础 detalength函数 字节的个数
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- sqlite中的自增主键
http://stackoverflow.com/questions/8519936/sqlite-autoincrement-primary-key-questions I'm not sure w ...
- Ubuntu 14.04中文输入法的安装
Ubuntu默认自带的中文输入法是IBUS框架的ibus-pinyin,IBUS-Bopomofo等.对于习惯于搜狗,紫光华宇,谷歌拼音的我们可能有点使用不习惯.下面就是安装常用的IBUS中文输入法. ...