HDU 2425 Hiking Trip(bfs+優先隊列)

Description

Hikingin the mountains is seldom an easy task for most people, as it is extremelyeasy to get lost during the trip. Recently Green has decided to go on a hikingtrip. Unfortunately, half way through the trip, he gets extremely tired and soneeds to find the path that will bring him to the destination with the leastamount of time. Can you help him?

You'veobtained the area Green's in as an R * C map. Each grid in the map can be oneof the four types: tree, sand, path, and stone. All grids not containing stoneare passable, and each time, when Green enters a grid of type X (where X can betree, sand or path), he will spend time T(X). Furthermore, each time Green canonly move up, down, left, or right, provided that the adjacent grid in thatdirection exists.

GivenGreen's current position and his destination, please determine the best pathfor him.

 

Input

Thereare multiple test cases in the input file. Each test case starts with twointegers 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, VT (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 Rlines 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 yourcurrent position and your destination. It is guaranteed that Green's currentposition is reachable – that is to say, it won't be a '@' square.

Thereis a blank line after each test case. Input ends with End-of-File.

 

Output

Foreach test case, output one integer on one separate line, representing theminimum amount of time needed to complete the trip. If there is no way forGreen to reach the destination, output -1 instead.

 

SampleInput

4 6

1 210

T...TT

TTT###

TT.@#T

..###@

0 13 0

 

4 6

1 22

T...TT

TTT###

TT.@#T

..###@

0 13 0

 

2 2

5 13

T@

@.

0 01 1

 

SampleOutput

Case1: 14

Case2: 8

Case3: -1


題意:有一R行C列的矩形區域,不同單位區域分佈着平路、沙路、樹林和石頭四種狀態,石頭不能走,經過其它三種狀態的時間不同,給出人的起始位置和終止位置,求出人能花費的最少時間。

分析:這是一道明顯的bfs求最短路徑的題目,從起始位置依次遍歷人能走的所有位置,用優先隊列保存各個狀態,遍歷到終點位置後返回最少時間。題目很直接,沒有什麼wa點。下面附上代碼。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
int r,c;
int vp,vs,vt;
int Move[4][2]={1,0,0,1,-1,0,0,-1};
int ans;
char s[25][25];
int sr,sc,tr,tc;
struct node  //創建結點,用x,y表示當前人的位置,num表示人當前所用時間。
{
int x,y,num; 
node(int xx=0,int yy=0,int nn=0):x(xx),y(yy),num(nn){}
friend bool operator<(const node &a,const node &b)
{
return a.num>b.num;
}
};
map<char,int>Map;

bool inx(int x)
{
return x>=0&&x<r;
}
bool iny(int y)
{
return y>=0&&y<c;
}
int bfs()
{
priority_queue<node>q;
s[sr][sc]='@';
q.push(node(sr,sc,Map[s[sr][sc]]));
//cout<<Map[s[sr][sc]]<<endl;
while(!q.empty())
{
node t=q.top(); q.pop();
if(t.x==tr&&t.y==tc) return t.num;
for(int i=0;i<4;i++)
{
int tx=t.x+Move[i][0];
int ty=t.y+Move[i][1];
if(inx(tx)&&iny(ty)&&s[tx][ty]!='@')
{
q.push(node(tx,ty,t.num+Map[s[tx][ty]]));
//cout<<Map[s[tx][ty]]<<endl;
s[tx][ty]='@';
}
}
}
return -1;
}
int main()
{
int T=1;
while(cin>>r>>c)
{
scanf("%d%d%d",&vp,&vs,&vt);
Map['#']=vp; Map['.']=vs; Map['T']=vt; Map['@']=0;
for(int i=0;i<r;i++)
scanf("%s",s[i]);
scanf("%d%d%d%d",&sr,&sc,&tr,&tc);
int ans=bfs();
printf("Case %d: %d\n",T++,ans);
}
return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章