题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4198

Quick out of the Harbour

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

bfs+优先队列。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::map;
using std::pair;
using std::vector;
using std::multimap;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
typedef unsigned long long ull;
bool vis[N][N];
char G[N][N];
int H, W, C, Sx, Sy, Dx, Dy;
const int dx[] = { , , -, }, dy[] = { -, , , };
struct Node {
int x, y, s;
Node(int i = , int j = , int k = ) :x(i), y(j), s(k) {}
inline bool operator<(const Node &a) const {
return s > a.s;
}
};
int bfs() {
priority_queue<Node> q;
q.push(Node(Sx, Sy, ));
vis[Sx][Sy] = true;
while (!q.empty()) {
Node t = q.top(); q.pop();
if (t.x == Dx && t.y == Dy) return t.s;
rep(i, ) {
int x = t.x + dx[i], y = t.y + dy[i];
if (x < || x >= H || y < || y >= W) continue;
if (G[x][y] == '#' || vis[x][y]) continue;
if (G[x][y] == '@') q.push(Node(x, y, t.s + C + ));
else if (G[x][y] == '.') q.push(Node(x, y, t.s + ));
vis[x][y] = true;
}
}
return -;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t;
scanf("%d\n", &t);
while (t--) {
scanf("%d %d %d", &H, &W, &C);
rep(i, H) {
scanf("%s", G[i]);
rep(j, W) {
vis[i][j] = false;
if (G[i][j] == 'S') Sx = i, Sy = j;
if (i == H - || i == || j == || j == W - ) {
if (G[i][j] != '#') Dx = i, Dy = j;
}
}
}
printf("%d\n", bfs() + );
}
return ;
}

hdu 4198 Quick out of the Harbour的更多相关文章

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

  2. HDU - 4198 Quick out of the Harbour (BFS+优先队列)

    Description Captain Clearbeard decided to go to the harbour for a few days so his crew could inspect ...

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

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

  4. hdu 4941 Magical Forest

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Description There is a forest can ...

  5. HDU 5868 Different Circle Permutation(burnside 引理)

    HDU 5868 Different Circle Permutation(burnside 引理) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=586 ...

  6. (hdu 6030) Happy Necklace 找规律+矩阵快速幂

    题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...

  7. hdu 5525 Product 数论算贡献

    Product Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Proble ...

  8. hdu 4432 数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=4432 6分钟写的代码,一上午去调试,, 哎,一则题目没看懂就去写了,二则,哎,,恶心了.在坚持几天然后ACM退役 ...

  9. [算法]——快速排序(Quick Sort)

    顾名思义,快速排序(quick sort)速度十分快,时间复杂度为O(nlogn).虽然从此角度讲,也有很多排序算法如归并排序.堆排序甚至希尔排序等,都能达到如此快速,但是快速排序使用更加广泛,以至于 ...

随机推荐

  1. Decks

    Now that we have Card objects, the next step is to define a class to represent decks. Since a deck i ...

  2. org.springframework.jdbc.BadSqlGrammarException

    org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: java.sql.SQLExc ...

  3. SVN中文件属性

    从SVN中checkout代码,然后设置crontab 定时执行脚本,发现permission denied 查看发现脚本没有可执行权限 但是,之前项目中的代码是有该权限的,于是猜想应该可以对SVN中 ...

  4. button的type属性

    今天为看懂一段js代码纠结了很久,搞不明白数据是如何实现post,因为button没有规定属性,其次对submit事件没太搞明白.忽然想起默认属性这个概念,豁然开朗,啊~ 1.请始终为按钮规定 typ ...

  5. 网络设备模拟器 GNS3

    https://www.gns3.com/support/docs/linux-installation sudo dpkg --add-architecture i386 sudo add-apt- ...

  6. CentOS 6.x 播放 mp3 音乐 —— 成功

    参考:http://blog.chinaunix.net/uid-14735472-id-3472898.html centos 6.x  添加 rpmforge 源--- CentOS 6.x 安装 ...

  7. undefined reference to `pthread_create'问题解决

    在编译pthread有关的程序时,出现undefined reference to `pthread_create'这样的错误. 问题原因: pthread 库不是 Linux 系统默认的库,连接时需 ...

  8. The given path's format is not supported.

    问题 编程以来今本没有使用input[type=file]这个控件过,今天突然使用尽然报错了,在本地chrome,firefox等其他的浏览器都是好的,唯独ie报错了.在服务器的时候,尽然chrome ...

  9. excel上传和下载

    需要注意的地方: 1.js构造表单并提交 2.js中文传参encodeURI(encodeURI("中文")),action接收并转换value = URLDecoder.deco ...

  10. kvm介绍

    KVM(Kernel-Based Virtual Machines)是一个基于Linux内核的虚拟化技术, 可以直接将Linux内核转换为Hypervisor(系统管理程 序)从而使得Linux内核能 ...