Enterprising Escape

这是一个关于解决星际迷航场景中企业号如何在最短时间内从克林贡包围中找到逃生路线的问题。输入包含一个矩形网格,标记了企业号和不同类型的克林贡战舰,每种战舰都有特定的击败时间。企业号必须沿着路径击败所有克林贡战舰才能到达边界。程序通过宽度和高度确定网格大小,然后寻找最短的逃脱时间并输出该时间。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值