Description

Captain Clearbeard decided to go to the harbour for a few days so his crew could inspect and repair the ship. Now, a few days later, the pirates are getting landsick(Pirates get landsick when they don't get enough of the ships' rocking
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

The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:

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

For every test case in the input, the output should contain one integer on a single line: the travelling time of the fastest route to open sea. There is always a route to open sea. Note that the open sea is not shown on the map, so
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

BAPC 2011

题意:求走出地图的最短时间,'#'不能走,'.'耗时一,'@'耗时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+优先队列)的更多相关文章

  1. hdu 4198 Quick out of the Harbour

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4198 Quick out of the Harbour Description Captain Cle ...

  2. 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 ...

  3. hdu 4198 Quick out of the Harbour(BFS+优先队列)

    题目链接:hdu4198 题目大意:求起点S到出口的最短花费,其中#为障碍物,无法通过,‘.’的花费为1 ,@的花费为d+1. 需注意起点S可能就是出口,因为没考虑到这个,导致WA很多次....... ...

  4. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  5. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  6. 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 ...

  7. 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 ...

  8. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  9. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

随机推荐

  1. Dapper,大规模分布式系统的跟踪系统

    概述 当代的互联网的服务,通常都是用复杂的.大规模分布式集群来实现的.互联网应用构建在不同的软件模块集上,这些软件模块,有可能是由不同的团队开发.可能使用不同的编程语言来实现.有可能布在了几千台服务器 ...

  2. WCF创建简单程序

    1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序.建立完成后如下图所示: 2.删除系统生成的两个文件IService1.cs与Service1.svc,当然你也可以直接 ...

  3. Matplotlib绘图双纵坐标轴设置及控制设置时间格式

    双y轴坐标轴图 今天利用matplotlib绘图,想要完成一个双坐标格式的图. fig=plt.figure(figsize=(20,15)) ax1=fig.add_subplot(111) ax1 ...

  4. 用于文本分类的RNN-Attention网络

    用于文本分类的RNN-Attention网络 https://blog.csdn.net/thriving_fcl/article/details/73381217 Attention机制在NLP上最 ...

  5. 基于PU-Learning的恶意URL检测

    https://xz.aliyun.com/t/2190 Ya-Lin Zhang, Longfei Li, Jun Zhou, Xiaolong Li, Yujiang Liu, Yuanchao ...

  6. discuz上传头像失败怎么解决

    刚安装好的discuz程序,可能需要我们做许多修改,而头像上传失败则是最为常见的问题之一,那么discuz上传头像失败怎么解决呢 进入ftp,打开跟目录下config文件 下载"config ...

  7. IntelliJ idea配置python

    为什么选择Intellij?因为我需要系统地管理python工程,Intellij可断点调试. 1.下载IntelliJ idea 在百度中搜索“IntelliJ idea”,并点击官网地址进行下载: ...

  8. [Canvas]动态背景

    欲查看动态效果请点击下载代码再用Chrome或Firefox打开index.html 图例: 代码: <!DOCTYPE html> <html lang="utf-8&q ...

  9. yii源码一 -- CComponent

    CComponent: path:framework/base/CComponent.php overview:This file contains the foundation classes fo ...

  10. Spring AspectJ切入点语法详解

    1.Spring AOP支持的AspectJ切入点指示符 切入点指示符用来指示切入点表达式目的,,在Spring AOP中目前只有执行方法这一个连接点,Spring AOP支持的AspectJ切入点指 ...