hdu2425Hiking Trip

来源:互联网 发布:python 主函数 传参数 编辑:程序博客网 时间:2024/06/06 01:20

Hiking Trip

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


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
 

Statistic | Submit | Discuss | Note

我最喜欢做这种搜索题了。

因为看到这种题很容易就能想到怎么做,用什么算法。

这题是典型的BFS加优先队列。

由于我对C++的优先队列操作不是很熟悉,所以就纯手工制作。


#include<stdio.h>#include<string.h>int map[23][23];int visit[23][23];int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};int row,line;int vp, vs, vt;int starti,startj,endi,endj;struct queue{int x,y;int va;};void Priqueue(int h,int t,struct queue q[]){struct queue b;int s = t;b = q[t];while(b.va<q[--s].va)q[s+1] = q[s];q[s+1] = b;}int BFS(){struct queue queue[19999];int tail,head,i;memset(visit,0,sizeof(visit));tail = head = 0;queue[tail].x = starti;queue[tail].y = startj;queue[tail++].va = 0;while(head<tail){int X = queue[head].x;int Y = queue[head].y;int V = queue[head++].va;if(X==endi && Y==endj)return V;for(i=0;i<4;i++)if(map[X+dir[i][0]][Y+dir[i][1]] && !visit[X+dir[i][0]][Y+dir[i][1]]){visit[X][Y] = 1;queue[tail].x = X+dir[i][0];queue[tail].y = Y+dir[i][1];queue[tail++].va = V+map[X+dir[i][0]][Y+dir[i][1]];Priqueue(head,tail-1,queue);}}return -1;}int main(){int i,j,ca=1;char ch[30];while(scanf("%d%d",&row,&line)!=EOF){scanf("%d%d%d",&vp,&vs,&vt);memset(map,0,sizeof(map));for(i=1;i<=row;i++){scanf("%s",ch+1);for(j=1;j<=line;j++){if(ch[j]=='#')map[i][j] = vp;else if(ch[j]=='.')map[i][j] = vs;else if(ch[j]=='T')map[i][j] = vt;}}scanf("%d%d%d%d",&starti,&startj,&endi,&endj);starti++,startj++,endi++,endj++;printf("Case %d: %d\n",ca++,BFS());}return 0;}


现在再加上用c++优先队列写的



#include<cstdio>#include<cstring>#include<queue>using namespace std;int map[23][23];int visit[23][23];int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};int row,line;int vp, vs, vt;int starti,startj,endi,endj;struct Q{int x,y;int va;bool operator < (const Q& a)const{return va>a.va;}};int BFS(){struct Q q,qq;priority_queue<Q> queue;int i;memset(visit,0,sizeof(visit));while(!queue.empty())queue.pop();q.x = starti;q.y = startj;q.va = 0;queue.push(q);visit[q.x][q.y] = 1;while(!queue.empty()){q = queue.top();queue.pop();if(q.x==endi && q.y==endj)return q.va;for(i=0;i<4;i++)if(map[q.x+dir[i][0]][q.y+dir[i][1]] && !visit[q.x+dir[i][0]][q.y+dir[i][1]]){visit[q.x+dir[i][0]][q.y+dir[i][1]] = 1;qq.x = q.x+dir[i][0];qq.y = q.y+dir[i][1];qq.va = q.va+map[q.x+dir[i][0]][q.y+dir[i][1]];queue.push(qq);}}return -1;}int main(){int i,j,ca=1;char ch[30];while(scanf("%d%d",&row,&line)!=EOF){scanf("%d%d%d",&vp,&vs,&vt);memset(map,0,sizeof(map));for(i=1;i<=row;i++){scanf("%s",ch+1);for(j=1;j<=line;j++){if(ch[j]=='#')map[i][j] = vp;else if(ch[j]=='.')map[i][j] = vs;else if(ch[j]=='T')map[i][j] = vt;}}scanf("%d%d%d%d",&starti,&startj,&endi,&endj);starti++,startj++,endi++,endj++;printf("Case %d: %d\n",ca++,BFS());}return 0;}



原创粉丝点击