zoukankan      html  css  js  c++  java
  • hdu 4198 Quick out of the Harbour

    题目连接

    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 = 510;
    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[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };
    34 struct Node {
    35     int x, y, s;
    36     Node(int i = 0, int j = 0, int k = 0) :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, 0));
    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, 4) {
    49             int x = t.x + dx[i], y = t.y + dy[i];
    50             if (x < 0 || x >= H || y < 0 || 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 + 1));
    53             else if (G[x][y] == '.') q.push(Node(x, y, t.s + 1));
    54             vis[x][y] = true;
    55         }
    56     }
    57     return -1;
    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
    ", &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 - 1 || i == 0 || j == 0 || j == W - 1) {
    74                     if (G[i][j] != '#') Dx = i, Dy = j;
    75                 }
    76             }
    77         }
    78         printf("%d
    ", bfs() + 1);
    79     }
    80     return 0;
    81 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    录毛线脚本,直接手写接口最简洁的LoadRunner性能测试脚本(含jmeter脚本)
    LoadRunner回放乱码
    结构体练习(C)
    MongoDB在windows及linux环境下安装
    pycharm 快捷键
    倒置输入的整数(C、Python)
    mysql常用操作(测试必备)
    wireshark配合jmeter测试webservice接口
    子网掩码的作用
    java 获取对象的数据类型、数据类型转换
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4628327.html
Copyright © 2011-2022 走看看