zoukankan      html  css  js  c++  java
  • hdu 2425 Hiking Trip

    题目连接

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

    Hiking Trip

    Description

    Hiking in the mountains is seldom an easy task for most people, as it is extremely easy to get lost during the trip. Recently Green has decided to go on a hiking trip. Unfortunately, half way through the trip, he gets extremely tired and so needs to find the path that will bring him to the destination with the least amount of time. Can you help him?
    You've obtained the area Green's in as an R * C map. Each grid in the map can be one of the four types: tree, sand, path, and stone. All grids not containing stone are passable, and each time, when Green enters a grid of type X (where X can be tree, sand or path), he will spend time T(X). Furthermore, each time Green can only move up, down, left, or right, provided that the adjacent grid in that direction exists.
    Given Green's current position and his destination, please determine the best path for him. 

    Input

    There are multiple test cases in the input file. Each test case starts with two integers R, C (2 <= R <= 20, 2 <= C <= 20), the number of rows / columns describing the area. The next line contains three integers, VP, VS, VT (1 <= VP <= 100, 1 <= VS <= 100, 1 <= VT <= 100), denoting the amount of time it requires to walk through the three types of area (path, sand, or tree). The following R lines describe the area. Each of the R lines contains exactly C characters, each character being one of the following: ‘T’, ‘.’, ‘#’, ‘@’, corresponding to grids of type tree, sand, path and stone. The final line contains four integers, SR, SC, TR, TC, (0 <= SR < R, 0 <= SC < C, 0 <= TR < R, 0 <= TC < C), representing your current position and your destination. It is guaranteed that Green's current position is reachable – that is to say, it won't be a '@' square.
    There is a blank line after each test case. Input ends with End-of-File.

    Output

    For each test case, output one integer on one separate line, representing the minimum amount of time needed to complete the trip. If there is no way for Green to reach the destination, output -1 instead.

    Sample Input

    4 6
    1 2 10
    T...TT
    TTT###
    TT.@#T
    ..###@
    0 1 3 0

    4 6
    1 2 2
    T...TT
    TTT###
    TT.@#T
    ..###@
    0 1 3 0

    2 2
    5 1 3
    T@
    @.
    0 0 1 1

    Sample Output

    Case 1: 14
    Case 2: 8
    Case 3: -1

    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 = 110;
    29 typedef unsigned long long ull;
    30 bool vis[N][N];
    31 char trip[N][N];
    32 const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };
    33 int R, C, Vs, Vp, Vt, Sx, Sy, Dx, Dy;
    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     cls(vis, false);
    43     priority_queue<Node> que;
    44     que.push(Node(Sx, Sy, 0));
    45     vis[Sx][Sy] = true;
    46     while (!que.empty()) {
    47         Node tmp = que.top(); que.pop();
    48         if (tmp.x == Dx && tmp.y == Dy) return tmp.s;
    49         rep(i, 4) {
    50             int nx = dx[i] + tmp.x, ny = dy[i] + tmp.y;
    51             char &ch = trip[nx][ny];
    52             if (nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
    53             if (ch == '@' || vis[nx][ny]) continue;
    54             if (ch == 'T') que.push(Node(nx, ny, tmp.s + Vt));
    55             else if (ch == '.') que.push(Node(nx, ny, tmp.s + Vs));
    56             else if (ch == '#') que.push(Node(nx, ny, tmp.s + Vp));
    57             vis[nx][ny] = true;
    58         }
    59     }
    60     return -1;
    61 }
    62 int main() {
    63 #ifdef LOCAL
    64     freopen("in.txt", "r", stdin);
    65     freopen("out.txt", "w+", stdout);
    66 #endif
    67     int k = 1;
    68     while (~scanf("%d %d", &R, &C)) {
    69         scanf("%d %d %d", &Vp, &Vs, &Vt);
    70         rep(i, R) scanf("%s", trip[i]);
    71         scanf("%d %d %d %d", &Sx, &Sy, &Dx, &Dy);
    72         printf("Case %d: %d
    ", k++, bfs());
    73     }
    74     return 0;
    75 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    Leetcode 剑指 Offer 27(二叉树的镜像)
    Leetcode 1022从根到叶的二进制之和
    Leetcode 993二叉树的堂兄弟节点
    Leetcode 965单值二叉树
    Leetcode 938 二叉搜索树的范围和
    hdu 2082 找单词
    母函数模板
    hdu 1398 Square Coins
    hdu 1085 Holding Bin-Laden Captive!
    hdu 1028 Ignatius and the Princess III
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4608903.html
Copyright © 2011-2022 走看看