CSU - 1815 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.

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
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<queue>
 4 #include<string.h>
 5 
 6 using namespace std;
 7 
 8 int c[1005][1005];
 9 int vis[1005][1005];
10 int d[30];
11 char e[30];
12 int dx[4]={-1,0,0,1};
13 int dy[4]={0,-1,1,0};
14 
15 int n,m,z,x1,y1,s;
16 char u;
17 
18 struct node
19 {
20     int x,y;
21     long long step;
22     friend bool operator < (node a,node b)
23     {
24         return a.step>b.step;
25     }
26 };
27 
28 
29 long long  BFS(int x2,int y2)
30 {
31     int x,y;
32     priority_queue<node> p;
33     node p1,p2;
34     p1.x=x2;p1.y=y2;
35     p1.step=0;
36     p.push(p1);
37     while(!p.empty())
38     {
39         p2=p.top();
40         p.pop();
41         x=p2.x;
42         y=p2.y;
43         if(x==1||y==1||x==n||y==m)
44             return p2.step;
45         for(int i=0;i<4;i++)
46         {
47             if(vis[p2.x+dx[i]][p2.y+dy[i]]==1)
48                 continue;
49             x=p2.x+dx[i];
50             y=p2.y+dy[i];
51                 p1.x=x;
52                 p1.y=y;
53                 p1.step=p2.step+c[x][y];
54                 vis[p1.x][p1.y]=1;
55                 p.push(p1);
56         }
57     }
58     return 0;
59 }
60 
61 int main()
62 {
63     int T;
64     scanf("%d",&T);
65     while(T--)
66     {
67     memset(vis,0,sizeof(vis));
68     scanf("%d%d%d",&z,&m,&n);
69     for(int i=1;i<=z;i++)
70         cin>>e[i]>>d[i];
71     for(int i=1;i<=n;i++)
72     {
73         getchar();
74         for(int j=1;j<=m;j++)
75         {
76             scanf("%c",&u);
77             //cout<<u; 
78             for(int q=1;q<=z;q++)
79                 if(u==e[q])
80                     c[i][j]=d[q];
81             if(u=='E')
82             {
83                 x1=i;
84                 y1=j;
85             }
86         }
87         //cout<<endl;
88     }
89     vis[x1][y1]=1;
90     //cout<<"wfa"<<endl;
91     long long s=BFS(x1,y1);
92     cout<<s<<endl;    
93     }
94     
95     return 0;
96 }

 

posted @ 2017-08-02 19:49  西北会法语  阅读(170)  评论(0编辑  收藏  举报