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

本博客探讨了在复杂地形中寻找最优路径的问题,通过优先队列和BFS算法,实现从出发点到目的地的最短时间路径计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叶孤心丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值