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