HDU - 4198 Quick out of the Harbour (BFS+优先队列)
Description
motion. That's why pirates often try to simulate that motion by drinking rum.). Before all of the pirates become too sick to row the boat out of the harbour, captain Clearbeard decided to leave the harbour as quickly as possible.
Unfortunately the harbour isn't just a straight path to open sea. To protect the city from evil pirates, the entrance of the harbour is a kind of maze with drawbridges in it. Every bridge takes some time to open, so it could be faster to take a detour. Your
task is to help captain Clearbeard and the fastest way out to open sea.
The pirates will row as fast as one minute per grid cell on the map. The ship can move only horizontally or vertically on the map. Making a 90 degree turn does not take any extra time.
Input
1. One line with three integers, h, w (3 <= h;w <= 500), and d (0 <= d <= 50), the height and width of the map and the delay for opening a bridge.
2.h lines with w characters: the description of the map. The map is described using the following characters:
―"S", the starting position of the ship.
―".", water.
―"#", land.
―"@", a drawbridge.
Each harbour is completely surrounded with land, with exception of the single entrance.
Output
you need to move outside of the map to reach open sea.
Sample Input
2
6 5 7
#####
#S..#
#@#.#
#...#
#@###
#.###
4 5 3
#####
#S#.#
#@..#
###@#
Sample Output
16
11
Source
题意:求走出地图的最短时间,'#'不能走,'.'耗时一,'@'耗时K+1
思路:在BFS的基础上加上优先队列
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 510; struct Point {
int x, y, step;
bool operator< (Point const &a) const {
return step > a.step;
}
} st, ed;
char map[MAXN][MAXN];
int vis[MAXN][MAXN];
int dx[4]={1, -1, 0, 0};
int dy[4]={0, 0, 1, -1};
int n, m, k; void bfs() {
memset(vis, 0, sizeof(vis));
priority_queue<Point> q;
vis[st.x][st.y] = 1;
q.push(st);
while (!q.empty()) {
Point cur = q.top();
q.pop();
if (cur.x == 0 || cur.x == n-1 || cur.y == 0 || cur.y == m-1) {
printf("%d\n", cur.step+1);
return;
}
for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (vis[nx][ny] || map[nx][ny] == '#')
continue;
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
vis[nx][ny] = 1;
Point tmp;
if (map[nx][ny] == '.') {
tmp.x = nx, tmp.y = ny;
tmp.step = cur.step + 1;
}
else if (map[nx][ny] == '@') {
tmp.x = nx, tmp.y = ny;
tmp.step = cur.step + k + 1;
}
q.push(tmp);
}
}
}
} int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; i++) {
scanf("%s", map[i]);
for (int j = 0; j < m; j++) {
if (map[i][j] == 'S') {
map[i][j] = '.';
st.x = i, st.y = j, st.step = 0;
}
}
}
bfs(); }
return 0;
}
HDU - 4198 Quick out of the Harbour (BFS+优先队列)的更多相关文章
- hdu 4198 Quick out of the Harbour
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4198 Quick out of the Harbour Description Captain Cle ...
- hdu 4198:Quick out of the Harbour解题报告
Quick out of the Harbour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu 4198 Quick out of the Harbour(BFS+优先队列)
题目链接:hdu4198 题目大意:求起点S到出口的最短花费,其中#为障碍物,无法通过,‘.’的花费为1 ,@的花费为d+1. 需注意起点S可能就是出口,因为没考虑到这个,导致WA很多次....... ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- hdu 1026 Ignatius and the Princess I(BFS+优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- hdu 3247 AC自动+状压dp+bfs处理
Resource Archiver Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Ot ...
随机推荐
- 【google chrome 一键打开 谷歌跳转的页面+JS Replace】谷歌无法打开网页的时候,提取网页中url的部分
经常在谷歌搜索,遇到网页无法打开,然后就停留在比如:http://www.google.com.hk/search?newwindow=1&safe=strict&site=& ...
- Libxml2函数及使用方法概述【转】
https://blog.csdn.net/chengwenyao18/article/details/7176082 一.关于XML: 在开始研究 Libxml2 库之前,先了解一下XML的相关基础 ...
- ListPopupWindow 列表弹窗 常见弹窗区别
案例 private void showPopupWindow(final Context context, @NonNull View anchorView) { final String[] po ...
- Maven WEB 项目使用ProGuard进行混淆,最佳解决方案
Maven WEB 项目使用ProGuard进行混淆,最佳解决方案 近期公司的Android项目做了混淆,虽说对于保护代码并不是100%的,但混淆后的代码可以使那些不法份子难以阅读,这样也能对代码的保 ...
- 7个提高效率的JavaScript调试工具
现在的JavaScript事实上已然成为了流行的web语言,即使它并不完美.很多程序员不喜欢用JavaScript写代码,是因为写到后来总会出现各种莫名其妙的bug,而且在开发大型应用程序的过程中很容 ...
- 基于Bootstrap+jQuery.validate Form表单验证实践
基于Bootstrap jQuery.validate Form表单验证实践 项目结构 : github 上源码地址:https://github.com/starzou/front-end- ...
- NLP系列(4)_朴素贝叶斯实战与进阶(转)
http://blog.csdn.net/han_xiaoyang/article/details/50629608 作者: 寒小阳 && 龙心尘 时间:2016年2月. 出处:htt ...
- VS2013第一个应用boost的程序
下载boost binary https://sourceforge.net/projects/boost/files/boost-binaries/1.67.0_b1/ 由于我用的是Windows1 ...
- USACO Arithmetic Progressions(暴力)
题目请点我 题解: 这道题的题意是找出集合里全部固定长度为N的等差数列.集合内的元素均为P^2+q^2的形式(0<=p,q<=M).时间要求5s内.本着KISS,直接暴力. 可是后来竟超时 ...
- GPUImage API 文档之GPUImageFilter类
GPUImageFilter类 方法 - (id)initWithVertexShaderFromString:(NSString *)vertexShaderString fragmentShade ...