Hiking Trip
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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.
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, V
P, V
S, V
T (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 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, 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 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.
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
求初始地点到目的地的最短时间,有各种符号"T",".","#"分别代表各种路径,并且"@"代表不通,其中不同的路需要的时间不同,使用优先队列每次从队列中跳出的都是目前最快的步骤,这样进行BFS就是要得到的解。
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#define INF 999999999
#define min(A,B)(A<B?A:B)
using namespace std;
int jx[] = {0,0,1,-1};
int jy[] = {1,-1,0,0};
int v[201][201];
char map[31][31];
int p1,p2,p3;
int n,m;
int x1,y1,y2,x2;
struct node
{
int x;
int y;
int cnt;
};
bool operator < (const node &a, const node &b)
{
return a.cnt > b.cnt;
}
void BFS()
{
memset(v,0,sizeof(v));
priority_queue<node>q;
while(!q.empty())
{
q.pop();
}
struct node t,f;
t.x = x1;
t.y = y1;
t.cnt = 0;
q.push(t);
v[t.x][t.y] = 1;
while(!q.empty())
{
t = q.top();
q.pop();
if(t.x == x2 && t.y == y2)
{
printf("%d\n",t.cnt);
return ;
}
for(int i=0;i<4;i++)
{
f.x = t.x + jx[i];
f.y = t.y + jy[i];
if(f.x>=0 && f.x<n && f.y>=0 && f.y<m && v[f.x][f.y] == 0 && map[f.x][f.y]!='@')
{
if(map[f.x][f.y] == '#')
{
f.cnt = t.cnt + p1;
}
else if(map[f.x][f.y] == '.')
{
f.cnt = t.cnt + p2;
}
else if(map[f.x][f.y] == 'T')
{
f.cnt = t.cnt + p3;
}
q.push(f);
v[f.x][f.y] = 1;
}
}
}
printf("-1\n");
}
int main()
{
int kk = 0;
while(scanf("%d%d",&n,&m)!=EOF)
{
kk++;
scanf("%d%d%d",&p1,&p2,&p3);
for(int i=0;i<n;i++)
{
scanf("%s",map[i]);
}
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("Case %d: ",kk);
BFS();
}
return 0;
}