题目连接

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. 第三方登录 QQ登录 人人网登录 新浪微博登录

    http://www.pp6.cn/Index.aspx http://www.pp6.cn/Login.aspx 网站有自己的账号系统,这里使用的第三方登录仅仅是获取第三方账号的唯一id,昵称,性别 ...

  2. php json中文处理方法,请json更懂中文

    1.php5.3版本及以下.的处理方式 /** *php5.3版本以前,json中文问题的解决解决方案 */ function encode_json($str) { return urldecode ...

  3. 匿名管道 远程cmd

    管道是单向的,传送数据的方向是固定的,所以互相通信需要两个管道. STARTUPINFO si; ZeroMemory(&si,sizeof(si)); si.dwFlags = STARTF ...

  4. MULTIBYTETOWIDECHAR的与WIDECHARTOMULTIBYTE的参数详解及相互转换

    第一个就是宽字符到多字节字符转换函数,函数原型如下: int WideCharToMultiByte( UINT CodePage, DWORD dwFlags, LPCWSTR lpWideChar ...

  5. cordova 日曆 聯系人 插件使用

    日曆插件 聯係人插件 我用聯係人插件, function onSuccess(contact) { alert("Save Success"); }; function onErr ...

  6. 样式重置 取消input默认样式

    body, h1, h2, h3, h4, h5, h6, hr, p,blockquote, dl, dt, dd, ul, ol, li,pre, form, fieldset, legend, ...

  7. js控制ul的显示隐藏,对象的有效范围

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 【改进版】C++小程序中一个cout输出语句背后的堆栈知识

    最开始写这篇文章的时候,凭着自己对汇编的一点理解就堆出了这些内容,经 egmkang的指点,才发觉自己是井底之蛙,花了半天的功夫,去学习顺序点等内容.针对上次写的程序,我决定添一些内容,把程序2后面的 ...

  9. Becoming a Hacker...

    This is my dream... http://catb.org/~esr/faqs/hacker-howto.html 黑客的精神 世上仍有大量迷人的事情等待解决 同样的问题不应被重复处理两次 ...

  10. MAC OS下免费下载YouTube

    YouTube上有很多不错的视频,你感兴趣的视频除了可以加入自己播放列表之外,还可以将其下载到本地收藏起来.推荐这款软件“Xilisoft Download YouTube Video for Mac ...