(CSU

来源:互联网 发布:成都数据分析咨询公司 编辑:程序博客网 时间:2024/06/06 12:44

(CSU - 1815)Enterprising Escape

Time Limit: 20 Sec Memory Limit: 1024 Mb Submitted: 366 Solved: 118

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

题目大意:有K个大写字母,每个字母有不同的分数(权值),用这些字母排成h行w列的矩阵,一个人从E字母处出发,每次只能上下左右走,问到达矩阵边界时所经过的路径分数的最小值。

思路:很显然可以用bfs求解,用dir[4][2]数组记录人走的四个不同的方向,用数组num[26]记录每一个字母所代表的权值v,又这里每一步路的权值不同,所以考虑用优先队列写bfs。

#include<cstdio>#include<cstring>#include<queue>using namespace std;const int maxn=1005;char a[maxn][maxn];bool vis[maxn][maxn];int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};int num[26];int k,w,h;int sx,sy;struct node{    int x,y,step;    friend bool operator< (node a,node b)    {        return a.step>b.step;    }};bool check(int x,int y){    return (x>=0&&x<h&&y>=0&&y<w);}int bfs(){    priority_queue<node> q;    node start,tmp,next;    start.x=sx;    start.y=sy;    start.step=0;    vis[sx][sy]=true;    q.push(start);    while(!q.empty())    {        tmp=q.top();        q.pop();        if(!check(tmp.x,tmp.y)) return tmp.step;        for(int i=0;i<4;i++)        {            next.x=tmp.x+dir[i][0];            next.y=tmp.y+dir[i][1];            if(!check(next.x,next.y)) return tmp.step;            if(check(next.x,next.y)&&!vis[next.x][next.y])            {                int cur=a[next.x][next.y]-'A';                next.step=tmp.step+num[cur];                vis[next.x][next.y]=true;                q.push(next);            }        }       }    return 0;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d%d\n",&k,&w,&h);        for(int i=0;i<k;i++)        {            char c;            int v;            scanf("%c %d\n",&c,&v);            num[c-'A']=v;        }        for(int i=0;i<h;i++) scanf("%s",a[i]);        for(int i=0;i<h;i++)            for(int j=0;j<w;j++)                if(a[i][j]=='E')                {                    sx=i;                    sy=j;                    break;                }        memset(vis,false,sizeof(vis));        printf("%d\n",bfs());    }    return 0;}