题目连接

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+优先队列。。

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<vector>
  7. #include<queue>
  8. #include<map>
  9. using std::cin;
  10. using std::cout;
  11. using std::endl;
  12. using std::find;
  13. using std::sort;
  14. using std::map;
  15. using std::pair;
  16. using std::vector;
  17. using std::multimap;
  18. using std::priority_queue;
  19. #define pb(e) push_back(e)
  20. #define sz(c) (int)(c).size()
  21. #define mp(a, b) make_pair(a, b)
  22. #define all(c) (c).begin(), (c).end()
  23. #define iter(c) decltype((c).begin())
  24. #define cls(arr,val) memset(arr,val,sizeof(arr))
  25. #define cpresent(c, e) (find(all(c), (e)) != (c).end())
  26. #define rep(i, n) for (int i = 0; i < (int)(n); i++)
  27. #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
  28. const int N = ;
  29. typedef unsigned long long ull;
  30. bool vis[N][N];
  31. char G[N][N];
  32. int H, W, C, Sx, Sy, Dx, Dy;
  33. const int dx[] = { , , -, }, dy[] = { -, , , };
  34. struct Node {
  35. int x, y, s;
  36. Node(int i = , int j = , int k = ) :x(i), y(j), s(k) {}
  37. inline bool operator<(const Node &a) const {
  38. return s > a.s;
  39. }
  40. };
  41. int bfs() {
  42. priority_queue<Node> q;
  43. q.push(Node(Sx, Sy, ));
  44. vis[Sx][Sy] = true;
  45. while (!q.empty()) {
  46. Node t = q.top(); q.pop();
  47. if (t.x == Dx && t.y == Dy) return t.s;
  48. rep(i, ) {
  49. int x = t.x + dx[i], y = t.y + dy[i];
  50. if (x < || x >= H || y < || y >= W) continue;
  51. if (G[x][y] == '#' || vis[x][y]) continue;
  52. if (G[x][y] == '@') q.push(Node(x, y, t.s + C + ));
  53. else if (G[x][y] == '.') q.push(Node(x, y, t.s + ));
  54. vis[x][y] = true;
  55. }
  56. }
  57. return -;
  58. }
  59. int main() {
  60. #ifdef LOCAL
  61. freopen("in.txt", "r", stdin);
  62. freopen("out.txt", "w+", stdout);
  63. #endif
  64. int t;
  65. scanf("%d\n", &t);
  66. while (t--) {
  67. scanf("%d %d %d", &H, &W, &C);
  68. rep(i, H) {
  69. scanf("%s", G[i]);
  70. rep(j, W) {
  71. vis[i][j] = false;
  72. if (G[i][j] == 'S') Sx = i, Sy = j;
  73. if (i == H - || i == || j == || j == W - ) {
  74. if (G[i][j] != '#') Dx = i, Dy = j;
  75. }
  76. }
  77. }
  78. printf("%d\n", bfs() + );
  79. }
  80. return ;
  81. }

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. docker学习笔记1 -- 安装和配置

    技术资料 docker中文官网:http://www.docker.org.cn/ 中文入门课程:http://www.docker.org.cn/book/docker.html docker学习笔 ...

  2. CLRS:master theory in complexity of algorithm

    T(n)=aT(n/b)+f(n); where we can interpret n/b to mean either floor(b/n) or ceil(b/n), Then T (n) has ...

  3. AX 获得当前Grid的数据源的记录行数

    sysQuery::CountTotal();方法 eg: int lines = sysQuery::CountTotal( SalesTable_ds.QueryRun());

  4. Linux下多线程编程

    一.为什么要引入线程? 使用多线程的理由之一是和进程相比,它是一种非常"节俭"的多任务操作方式.在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维 ...

  5. C++中的位运算总结

    1)位运算 位运算是指对转换成二进制的数字进行每一位上的0.1的运算,运算涉及到五种运算:与(&),或(|),异或(^),左移(<<),右移(>>). 如下表所示:   ...

  6. 使用扩展方法(this 扩展类型)

    namespace ConsoleApp_UseExtendWays{ class Program { static void Main(string[] args) { Student s = ne ...

  7. hdu2078

    刚开始看这题,感觉是DP什么的 ,后来我发现,只要找到中最小值,就可以啦,哈哈.假如用x1把0-100分割. 则0-x1-100  ===>   x1^2+(100-x1)^2 跟0-100   ...

  8. SpringMVC序列化Long转成String

    问题:由于JS中Number的精度为16位(最大位17位,第17位精度不准),我们的ID用的Number 18位,传到客户端会丢失最后两位: 解决方式:Long序列化成String,传到客户端: 注意 ...

  9. SQL更新表的字段

    Oracle: alter table CAPIAGENTLOG modify(clientcode nvarchar2()) SQL Server: alter table CAPIAGENTLOG ...

  10. Android数据库(sqlite)加密方案

    最近因为一些项目的安全性需要将数据库加密,一开始想到的就是先将数据库通过AES加密,然后运行时再解密,另一种是将数据库里的内容加密. 很快这两种方案都是不理想的,第一种加密方式形同虚设,第二种,如果加 ...