csu1815

 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.

Sample 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
Sample Output
2
400
 
 
解题思路:
题意就可以看样例,字母做棋盘,每个字母都是有值的,要求最后走出来的,也就是到边缘的时候,值最小,bfs
代码:
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int vis[1005][1005];
char a[1005][1005];
int value[30];
int k,w,h;
int dis[][2] = {{0,1},{1,0},{0,-1},{-1,0}};
struct node
{
    int x;
    int y;
    int sum;
    bool operator < (const node& a) const
    {
        return sum>a.sum;
    }

};
node beginn;
int bfs(node now)
{
    priority_queue<node> q;
    q.push(now);
    vis[now.x][now.y] = 1;
    node help;
    node help1;
    while(!q.empty())
    {
        help = q.top();
        q.pop();
        if(help.x<=0||help.x>=w||help.y<=0||help.y>=h)
        {
          //  cout<<"w "<<w<<"h "<<h<<endl;
            return help.sum;
        }

        for(int i = 0;i<4;i++)
        {
            help1.x = help.x+dis[i][0];
            help1.y = help.y+dis[i][1];
            char pos = a[help1.x][help1.y];
            help1.sum = help.sum+value[pos-'A'];
            if(!vis[help1.x][help1.y])
            {
                q.push(help1);
                vis[help1.x][help1.y] = 1;
            }
        }
    }
    return -1;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(a,0,sizeof(a));
        memset(value,0,sizeof(value));
        memset(vis,0,sizeof(vis));
        cin>>k>>h>>w;
        for(int i = 0;i<k;i++)
        {
            char help1;
            int help2;
            cin>>help1>>help2;
            value[help1-'A'] = help2;
        }
        for(int i = 0;i<w;i++)
            for(int j = 0;j<h;j++)
        {
            cin>>a[i][j];
            if(a[i][j]=='E')
            {
                beginn.x = i;
                beginn.y = j;
                beginn.sum = 0;
            }
        }

       cout<< bfs(beginn)<<endl;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值