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

来源:互联网 发布:淘宝达人资历说明案例 编辑:程序博客网 时间:2024/05/20 14:17

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, 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 61 2 10T...TTTTT###TT.@#T..###@0 1 3 04 61 2 2T...TTTTT###TT.@#T..###@0 1 3 02 25 1 3T@@.0 0 1 1
 

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

Source
2008 Asia Hangzhou Regional Contest Online
 

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


0 0
原创粉丝点击