CSU 1815:Enterprising Escape(BFS+优先队列)

Enterprising Escape

Time limit:20000 ms Memory limit:1048576 kB


Problem Description

    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

题意:

Enterprise 被一群 Klingon 包围了,Enterprise现在要杀出一条血路,已知需要和每种 Klingon 战斗所需的时间,Enterprise可以走上下左右四个方向,问Enterprise走到边界所需的最短时间。

解题思路:

直接BFS。记得清vis数组。。~第二次写错方向数组了~


Code:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

const int maxn=1000+5;
int k,w,h;
char mp[maxn][maxn];
int vis[maxn][maxn];
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
struct Node
{
    int x,y,step;
    friend bool operator < (Node a,Node b)
    {
        return a.step>b.step;
    }
};

bool check(int x,int y)
{
    if(x<0||x>=h||y<0||y>=w)
        return false;
    if(vis[x][y])
        return false;
    return true;
}

priority_queue<Node> Q;
map<char,int> M;

int BFS(int x1,int y1)
{
    int ans=1e9;
    while(!Q.empty())
        Q.pop();
    vis[x1][y1]=1;
    Q.push(Node{x1,y1,0});
    while(!Q.empty())
    {
        Node u=Q.top();
        //cout<<"pop: "<<u.x<<' '<<u.y<<' '<<u.step<<endl;
        if(u.x<=0||u.x>=h-1||u.y<=0||u.y>=w-1)
        {
            return u.step;
        }
        Q.pop();
        for(int i=0;i<4;i++)
        {
            int x=u.x+dx[i];
            int y=u.y+dy[i];
            if(check(x,y))
            {
                vis[x][y]=1;
                //cout<<"push: "<<x<<' '<<y<<' '<<u.step+M[mp[x][y]]<<endl;
                Q.push(Node{x,y,u.step+M[mp[x][y]]});
            }
        }
    }
    return 0;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&k,&w,&h);
        M.clear();
        char c;
        int num;
        for(int i=0;i<k;i++)
        {
            getchar();
            scanf("%c%d",&c,&num);
            M[c]=num;
        }
        for(int i=0;i<h;i++)
            scanf("%s",mp[i]);
        int x1,y1;
        for(int i=0;i<h;i++)
        {
            for(int j=0;j<w;j++)
            {
                if(mp[i][j]=='E')
                {
                    x1=i;
                    y1=j;
                    break;
                }
            }
        }
        mem(vis,0);
        printf("%d\n",BFS(x1,y1));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值