Enterprising Escape
The Enterprise is surrounded by Klingons! Find the escape route that has the quickest exit
time, and print that time.
Input is a rectangular grid; each grid square either has the Enterprise or some class of a Klingon
warship. Associated with each class of Klingon warship is a time that it takes for the Enterprise
to defeat that Klingon. To escape, the Enterprise must defeat each Klingon on some path to the
perimeter. Squares are connected by their edges, not by corners (thus, four neighbors).
Input
The first line will contain T, the number of cases; 2 ≤ T ≤ 100. Each case will start with line
containing three numbers k, w, and h. The value for k is the number of different Klingon classes
and will be between 1 and 25, inclusive. The value for w is the width of the grid and will be
between 1 and 1000, inclusive. The value for h is the height of the grid and will be between 1 and
1000, inclusive.
Following that will be k lines. Each will consist of a capital letter used to label the class of
Klingon ships followed by the duration required to defeat that class of Klingon. The label will not
be “E”. The duration is in minutes and will be between 0 and 100,000, inclusive. Each label will
be distinct.
Following that will be h lines. Each will consist of w capital letters (with no spaces between
them). There will be exactly one “E” across all h lines, denoting the location of the Enterprise; all
other capital letters will be one of the k labels given above, denoting the class of Klingon warship
in the square.
Output
Your output should be a single integer value indicating the time required for the Enterprise to
escape.
2013 Pacific Northwest Region Programming Contest 13
Sample Input Sample Output
Input
2
6 3 3
A 1
B 2
C 3
D 4
F 5
G 6
ABC
FEC
DBG
2 6 3
A 100
B 1000
BBBBBB
AAAAEB
BBBBBB
output
2
400
#include<stdio.h>
#include<queue>
#include<math.h>
using namespace std;
struct node
{
int x,y;
int val;
}u,v;
char st[11],c;
int val[27],vis[1001][1001];
queue<node>q;
int nex[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char mp[1001][1001];
int main()
{
int i,j;
int n,m,k,l,va,sx,sy,xx,yy,mid=0;
int t;
int mini=1000000001;
scanf("%d",&t);
for(int kcase=0;kcase<t;kcase++)
{
mini=1000000001;
while(!q.empty())
{
q.pop();
}
scanf("%d %d %d",&k,&m,&n);
fgets(st,11,stdin);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
vis[i][j]=100000000;
}
}
for(i=0;i<k;i++)
{
scanf("%c %d",&c,&va);
val[c-'A']=va;
fgets(st,11,stdin);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='E')
{
sx=i,sy=j;
}
}
fgets(st,11,stdin);
}
mid=sx;
mid=min(mid,n-1-sx);
mid=min(sy,mid);
mid=min(mid,m-1-sy);
u.x=sx,u.y=sy;
u.val=0;
q.push(u);
while(!q.empty())
{
v=q.front();
q.pop();
if(v.x==0||v.y==0||v.x==n-1||v.y==m-1)
{
if(v.val<mini)
{
mini=v.val;
}
continue;
}
if(v.val>=mini||v.val>100001*mid)
{
continue;
}
if(v.val>vis[v.x][v.y])
{
continue;
}
for(i=0;i<4;i++)
{
xx=v.x+nex[i][0];
yy=v.y+nex[i][1];
if(xx<0||yy<0||xx>=n||yy>=m)
{
continue;
}
u.val=v.val+val[mp[xx][yy]-'A'];
if(u.val<vis[xx][yy]&&u.val<mini&&u.val<100001*mid)
{
u.x=xx;
u.y=yy;
vis[xx][yy]=u.val;
q.push(u);
}
}
}
printf("%d\n",mini);
}
return 0;
}