/*
分析:
简单广搜。
坑啊~,每组后面不用输出那个空行的。。。
接近半个月没怎么刷题了,惭愧呀~~~
2012-08-16 12:03
*/
分析:
简单广搜。
坑啊~,每组后面不用输出那个空行的。。。
接近半个月没怎么刷题了,惭愧呀~~~
2012-08-16 12:03
*/
#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
int r,c;
int base[4];
int map[25][25];
int flag[25][25];
int x_s,y_s;
int x_e,y_e;
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};
struct node
{
int x,y;
int step;
friend bool operator<(node n1,node n2)
{
return n2.step<n1.step;
}
};
int judge(int x,int y)
{
if(x<0 || x>=r || y<0 || y>=c) return 1;
if(map[x][y]==-1) return 1;
if(flag[x][y]) return 1;
return 0;
}
int BFS()
{
priority_queue<node>q;
node now,next;
int i;
memset(flag,0,sizeof(flag));
now.x=x_s;
now.y=y_s;
now.step=0;
flag[now.x][now.y]=1;
q.push(now);
while(!q.empty())
{
now=q.top();
q.pop();
if(now.x==x_e && now.y==y_e) return now.step;
for(i=0;i<4;i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
if(judge(next.x,next.y)) continue;
next.step=now.step+base[map[next.x][next.y]];
flag[next.x][next.y]=1;
q.push(next);
}
}
return -1;
}
int main()
{
int Case=1;
int i,l;
char str[25];
while(scanf("%d%d",&r,&c)!=-1)
{
scanf("%d%d%d",&base[1],&base[2],&base[3]);
for(i=0;i<r;i++)
{
scanf("%s",str);
for(l=0;str[l];l++)
{
if(str[l]=='#') map[i][l]=1;
else if(str[l]=='.')map[i][l]=2;
else if(str[l]=='T')map[i][l]=3;
else if(str[l]=='@')map[i][l]=-1;
}
}
scanf("%d%d%d%d",&x_s,&y_s,&x_e,&y_e);
printf("Case %d: %d\n",Case++,BFS());
}
return 0;
}