HDU 2425 Hiking Trip(bfs+优先队列)

28 篇文章 0 订阅
20 篇文章 0 订阅

Hiking Trip

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1582    Accepted Submission(s): 696


Problem 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
 

Source
 

Recommend
lcy

题目大意:
    在一个地图上有4种地形,人只能向上下左右四个方向移动,除了石头都可以走,经过不同的地形所需要的时间不同。求从起点到终点所需要的最短时间,如果无法到达输出-1。

解题思路:
    因为通过不同地形所需时间不同,所以要使用优先队列使时间短的点先出列,而且由于同一个点可能有不同路径到达所用的时间不同,所以需要一个vis二维数组维护到达一个点所需的最短时间。同时也可以通过这个vis数组进行剪枝,当到达这个点的时间大于以前到这个点的最短时间就走进这个点,否则剪掉。

附AC代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;

struct node//优先队列中储存状态的节点
{
    int i,j,t;//坐标,时间
    node(int ii,int jj,int tt):i(ii),j(jj),t(tt){}
    bool operator<(const node& other)const{return t>other.t;}//自定义小于号使时间小的先出列
};

const int di[]={-1,1,0,0},dj[]={0,0,-1,1};//移动的4个方向
const int maxn=20+5;
char land[maxn][maxn];//记录地图
int r,c,vp,vs,vt,si,sj,ei,ej;
int vis[maxn][maxn];//维护到达某一点所需的最短时间

int get_time(int i,int j)//得到经过这一点所需的时间
{
    if(land[i][j]=='T')
        return vt;
    else if(land[i][j]=='.')
        return vs;
    return vp;
}
int main()
{
    int T=1;
    while(~scanf("%d%d",&r,&c))
    {
        memset(vis,0x3f,sizeof vis);//初始化
        scanf("%d%d%d",&vp,&vs,&vt);
        for(int i=0;i<r;++i)//读入地图
            scanf("%s",land[i]);
        scanf("%d%d%d%d",&si,&sj,&ei,&ej);//输入起点坐标,终点坐标
        priority_queue<node> q;
        q.push(node(si,sj,0));//起点加入优先队列
        vis[si][sj]=0;
        int ans=-1;
        while(!q.empty())
        {
            node x=q.top();
            q.pop();
            if(x.i==ei&&x.j==ej)//到达终点
            {
                ans=x.t;
                break;
            }
            for(int i=0;i<4;++i)
            {
                int ni=x.i+di[i],nj=x.j+dj[i],nt=x.t;
                if(ni<0||ni>=r||nj<0||nj>=c||land[ni][nj]=='@')
                    continue;
                nt+=get_time(ni,nj);
                if(vis[ni][nj]<=nt)//如果之前已经有到达这一点个更优解
                    continue;
                vis[ni][nj]=nt;//更新
                q.push(node(ni,nj,nt));
            }
        }
        printf("Case %d: %d\n",T++,ans);
    }
    
    return 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值