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. RV32M指令集

    Risc-V定义了可选的RV32M,它定义了整数乘法除法指令. 总共8条指令. Category Fmt RV32I base machine code(bin) comment mul 整数乘法 R ...

  2. 第三章 dubbo内核之ioc源码解析

    dubbo的IOC具体实现在:T injectExtension(T instance)方法中.该方法只在三个地方被使用: createAdaptiveExtension() --injectExte ...

  3. jQuery easyui layout布局自适应浏览器大小(转)

    首先解释一下标题的含义,当我们用jQuery easyui layout 进行布局的时候,可能会遇到这样一个问题,那就是当手工调整浏览器大小,或者最大化.还原窗口的时候,layout的某个区域不能填充 ...

  4. Win32进程创建、进程快照、进程终止用例

    进程创建: 1 #include <windows.h> #include <stdio.h> int main() { // 创建打开系统自带记事本进程 STARTUPINF ...

  5. 用Telnet测试服务器的端口是否开通

      可以用telnet测试远程服务器的端口是否开通,格式如下: telnet <server name> <port number> 例如: Telnet tserv 3389 ...

  6. 命令行下从bak文件恢复sqlserver数据库方法

    命令行下从bak文件恢复sqlserver数据库方法 注:本文所示访问从SqlServer 2000 - 2014版都是通用的 参考:http://blog.sina.com.cn/s/blog_5c ...

  7. [React] Use React.ReactNode for the children prop in React TypeScript components and Render Props

    Because @types/react has to expose all its internal types, there can be a lot of confusion over how ...

  8. angular5中使用jsonp请求页面

    说多了,都是眼泪,折腾了很久,各种百度,查到的例子,全都报错,可能是因为我的angular的版本太高,向下都不兼容? 我的angular版本为最新的5.2.11: 下面是正确的jsonp请求的方法 1 ...

  9. ZH奶酪:LAMP环境中如何重新部署一个Yii2.0 web项目

    使用Yii2.0 framework开发的项目,使用Github进行版本控制,现在要把这个项目部署到一个新的电脑/系统中: (1)安装LAMP (2)在/var/www/html目录下执行 git c ...

  10. You have version null and I want version 8

    删除hdfs上的/hbasehadoop fs -rm -r /hbase 删除zookeeper上的/hbasezookeeper-client -server 192.168.1.2:2181 r ...