HDU-2425-Hiking Trip(优先队列+BFS)

Hiking Trip

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, 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
Source

2008 Asia Hangzhou Regional Contest Online

解题思路:

简单BFS,但是需要加优先队列优化,不然就会找到错误答案。
ps:学到了一种新的优先队列比较函数的写法,结构体内重载运算符。

AC代码:

#include <iostream>
#include <cstdio>
#include <math.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = 1e6+10;
const int mod = 1000000007;
struct node{
    int x,y;
    int time;
    node(){}
    node(int a,int b,int c)
    {
        x = a;
        y = b;
        time = c;
    }
    friend bool operator <(const node & a1, const node & a2)
    {
        return a1.time > a2.time;
    }
}pos;

struct cmp{
    operator()(const node a1,const node a2)
    {
        return a1.time > a2.time;
    }
};

char mp[25][25];
int vis[25][25];
int cost[25][25];

int main()
{
    ll n,m;
    ll vp,vs,vt;
    ll sx,sy,dx,dy;
    ll cas =1;
    ll dir[4][2] = {0,1,0,-1,1,0,-1,0};
    while(cin>>n>>m)
    {
        cin>>vp>>vs>>vt;
        for(int i = 0 ;i  < n ; i  ++)
            for(int j = 0 ; j < m ; j ++)
                cin>>mp[i][j];
        for(int i = 0 ;i  < n ; i  ++)
            for(int j = 0 ; j < m ; j ++)
            {
                if(mp[i][j] == '@')
                    cost[i][j] = -1;
                if(mp[i][j] == 'T')
                    cost[i][j] = vt;
                if(mp[i][j] == '.')
                    cost[i][j] = vs;
                if(mp[i][j] == '#')
                    cost[i][j] = vp;
            }

        cin>>sx>>sy>>dx>>dy;
        memset(vis,0,sizeof(vis));
        vis[sx][sy] = 1;
        priority_queue<node> que;
        que.push(node(sx,sy,0));
        ll ans = -1;
        while(!que.empty())
        {
            pos = que.top();
            que.pop();
            if(pos.x == dx && pos.y == dy)
            {
                ans = pos.time;
                break;
            }
            for(int i = 0 ; i < 4 ; i ++)
            {
                int tmpx = pos.x + dir[i][0];
                int tmpy = pos.y + dir[i][1];
                if(tmpx >= 0 && tmpx < n && tmpy >= 0 && tmpy < m && mp[tmpx][tmpy] != '@' && !vis[tmpx][tmpy])
                {
                    vis[tmpx][tmpy] = 1;
                    que.push(node(tmpx,tmpy,pos.time + cost[tmpx][tmpy]));
                }
            }
        }
        cout<<"Case "<<cas++<<": ";
        cout<<ans<<endl;
    }
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值