HDU 2425 Hiking Trip(搜索+优先队列)

Hiking Trip
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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, V  P, V  S, V  T (1 <= V  P <= 100, 1 <= V  S <= 100, 1 <= V  T <= 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, S  R, S  C, T  R, T  C, (0 <= S  R < R, 0 <= S  C < C, 0 <= T  R < R, 0 <= T  C < 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
 



求初始地点到目的地的最短时间,有各种符号"T",".","#"分别代表各种路径,并且"@"代表不通,其中不同的路需要的时间不同,使用优先队列每次从队列中跳出的都是目前最快的步骤,这样进行BFS就是要得到的解。


代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<queue>

#define INF 999999999
#define min(A,B)(A<B?A:B)

using namespace std;

int jx[] = {0,0,1,-1};
int jy[] = {1,-1,0,0};
int v[201][201];
char map[31][31];
int p1,p2,p3;
int n,m;
int x1,y1,y2,x2;

struct node
{
    int x;
    int y;
    int cnt;

};

bool operator < (const node &a, const node &b)
{
    return a.cnt > b.cnt;
}

void BFS()
{
    memset(v,0,sizeof(v));
    priority_queue<node>q;
    while(!q.empty())
    {
        q.pop();
    }
    struct node t,f;
    t.x = x1;
    t.y = y1;
    t.cnt = 0;
    q.push(t);
    v[t.x][t.y] = 1;
    while(!q.empty())
    {
        t = q.top();
        q.pop();
        if(t.x == x2 && t.y == y2)
        {
            printf("%d\n",t.cnt);
            return ;
        }
        for(int i=0;i<4;i++)
        {
            f.x = t.x + jx[i];
            f.y = t.y + jy[i];
            if(f.x>=0 && f.x<n && f.y>=0 && f.y<m && v[f.x][f.y] == 0 && map[f.x][f.y]!='@')
            {
                if(map[f.x][f.y] == '#')
                {
                    f.cnt = t.cnt + p1;
                }
                else if(map[f.x][f.y] == '.')
                {
                    f.cnt = t.cnt + p2;
                }
                else if(map[f.x][f.y] == 'T')
                {
                    f.cnt = t.cnt + p3;
                }
                q.push(f);
                v[f.x][f.y] = 1;
            }
        }
    }
    printf("-1\n");
}

int main()
{
    int kk = 0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        kk++;
        scanf("%d%d%d",&p1,&p2,&p3);
        for(int i=0;i<n;i++)
        {
            scanf("%s",map[i]);
        }
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        printf("Case %d: ",kk);
        BFS();
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值