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, TR, 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.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
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct point
{
int x,y;
int time;
point(int xx,int yy,int tt):x(xx),y(yy),time(tt){
}
friend operator < (point a,point b)//重载<运算符,让time小的在前
{
return a.time > b.time;
}
};
int main(void)
{
int r,c;
int acount=0;
while(~scanf("%d %d",&r,&c))
{
priority_queue<point> q;
char arr[25][25];
int in[25][25];
memset(in,0,sizeof(in));
memset(arr,'@',sizeof(arr));
int path,sand,tree;
scanf("%d %d %d",&path,&sand,&tree);
int i,j;
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
scanf(" %c",&arr[i][j]);
}
}
int x,y;
scanf("%d %d",&x,&y);
q.push(point(x+1,y+1,0));
scanf("%d %d",&x,&y);
x=x+1;
y=y+1;
int dx[4]={1,0,-1,0};//方向
int dy[4]={0,1,0,-1};
while(q.size())
{
point now=q.top();//注意priority_queue不能用front或back
if(now.x==x&&now.y==y)
{
printf("Case %d: %d\n",++acount,now.time);
break;
}
q.pop();
for(i=0;i<4;i++)
{
int xx=dx[i]+now.x;
int yy=dy[i]+now.y;
if(in[xx][yy]==0&&arr[xx][yy]!='@')
{
in[xx][yy]=1;
if(arr[xx][yy]=='#')
{
q.push(point(xx,yy,now.time+path));
}
else if(arr[xx][yy]=='.')
{
q.push(point(xx,yy,now.time+sand));
}
else
{
q.push(point(xx,yy,now.time+tree));
}
}
}
}
if(q.empty())
printf("Case %d: -1\n",++acount);
}
return 0;
}