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 ...
随机推荐
- STM32中基于DMA的ADC采样实例之MQ-2烟雾传感器
最近学习了一下STM32中的ADC采样,由于手头正好有一个MQ-2的烟雾传感器,所以正好可以测试一把.体验ADC采样的过程.下面介绍一下这个MQ-2烟雾传感器. 1.MQ-2烟雾传感器简介 MQ-2气 ...
- 用于HTML5移动开发的10大移动APP开发框架【转】
今天给大家介绍10款有关HTML5移动开发APP开发框架,这几款框架都是比较优秀的移动 Web 开发框架,能够帮助开发者更加高效的开发移动Web应用.. 十款移动APP开发框架: 1.jquery m ...
- Gridview利用DataFormatString属性设置数据格式
首 先把Gridview的AutoGenerateColumns属性设为False(默认是False),DataField选择相应的字段,特别需要注 意的是要把需要设置的字段的HtmlEncode属性 ...
- JS获取当前项目名
代码如下: //获取当前网址,如: var curWwwPath=window.document.location.href; //获取主机地址之后的目录如:/Tmall/index.jsp var ...
- 解决EditPlus在设置了UTF-8之后,编写的HTML页面仍出现汉字乱码问题
解决EditPlus在设置了UTF-8之后.编写的HTML页面仍出现汉字乱码问题 相信有些同学在使用EditPlus编写HTML页面时发现,尽管已经设置好了UTF-8的编码格式.但却发现HTML页 ...
- Android -- Canvas java.lang.UnsupportedOperationException
干货 java.lang.UnsupportedOperationException at android.view.GLES20Canvas.clipPath(GLES20Canvas.java:2 ...
- linux命令学习——file
1.简介 file命令是用来检测并显示文件类型(determine file type).通过file指令,我们得以辨识该文件的类型,例如可以知道动态连接库是32位还是64位. 2.命令格式 file ...
- RAMPS1.4 3d打印控制板接线与测试3
RAMPS1.4作为mega2560的拓展板插在mega板子上面.从而让mega板子可以控制3d打印机的工作.ramps上的接线至关重要,接错不仅不能打印,甚至还会烧坏器件和板子.请一定注意. 我的淘 ...
- VMware中创建Ubuntu16.0.4虚拟桥连方式无法上网
一.问题描述 在本地VMvare中已经安装了两台虚拟机,网络方式都是桥连,上网都可以自动获取IP地址 和HOST主机是一个号段的 ,同为192.168.1.X KingServer1(原始安装) 桥 ...
- libsvm使用说明
http://www.hankcs.com/ml/libsvm-usage.html libsvm使用说明 码农场 > 机器学习 2016-02-18 阅读(345) 评论(0) 目录 l ...