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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define inf 0x3fffffff
using namespace std;
const int maxn=;
int n,c,r;
char cc;
int cost;
char a[maxn][maxn];
int vis[maxn][maxn];
int dir[][]={ {,},{,},{-,},{,-} }; 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<||x>=r||y<||y>=c||vis[x][y])//横纵方向超届+已经访问
return false;
return true;
} priority_queue<Node> q;
map<char,int> mp; int dfs(int x1,int y1)
{
while(!q.empty()) q.pop();//清空队列
vis[x1][y1]=;//清空标记数组
q.push(Node{x1,y1,});//给x/y/step赋初值并且插入队列 while(!q.empty())
{
Node u=q.top();//另添结构体节点u 取队首值
q.pop();//弹出队首
if(u.x<=||u.x>=r-||u.y<=||u.y>=c-)//达到条件
return u.step; for(int i=;i<;i++) //遍历四个方向
{
int x=u.x+dir[i][];
int y=u.y+dir[i][]; if(check(x,y)) //检查边界符合
{
vis[x][y]=; //标记访问
q.push(Node{x, y, u.step+mp[a[x][y]]}); //插入新的横纵节点,步数每次增加数值为map的键值
}
}
}
return ;
} int main()
{
int t;
int x1,y1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&c,&r);
mp.clear();//注意!!
for(int i=;i<n;i++)
{
getchar();//注意!!
scanf("%c %d",&cc,&cost);
mp[cc]=cost;
}
for(int i=;i<r;i++)
scanf("%s",&a[i]);
for(int i=;i<r;i++)
{
for(int j=;j<c;j++)
{
if(a[i][j]=='E')
{
x1=i;
y1=j;
break;
}
}
}
memset(vis,,sizeof(vis));
printf("%d\n",dfs(x1,y1));
}
return ;
}

最新文章

  1. mongodb安装&amp;简单使用
  2. 高性能 TCP/UDP/HTTP 通信框架 HP-Socket v4.1.2
  3. 炉石传说 C# 开发笔记(BS上线尝试)
  4. mysql主从数据库不同步的2种解决方法(转)
  5. 安装uwsgi记录
  6. 使用SQL对数据进行整理
  7. .Net 2.0自带的Json序列化、反序列化方法
  8. MySQL与Oracle 差异比较之一数据类型
  9. iOS和android游戏纹理优化和内存优化(cocos2d-x)
  10. Penetration test
  11. &lt;meta http-equiv=&quot;Pragma&quot; content=&quot;no-cache&quot;&gt;
  12. Visual Studio 2013发布Cloud Service至Azure China
  13. /usr 的由来及/usr目录结
  14. nginx 拦截 swagger 登录
  15. Android常用学习网站
  16. 企点微服务网关演进之路 IT大咖说 - 大咖干货,不再错过
  17. linux系统环境搭建
  18. HTML基础标签的综合应用案例(颜色、斜体、加粗、下划线、a标签、无序列表、有序列表)
  19. python16_day39【算法】
  20. iOS中 imageNamed方法 非常多图片占用大量内存问题

热门文章

  1. [ZJOI2008]骑士 DP dfs
  2. CF893F Subtree Minimum Query 解题报告
  3. 大学本科毕业论文——LanguageTool语法校正规则库的开发
  4. mysql共享表空间和独立表空间
  5. 移动端浏览器touch事件的研究总结
  6. TCP之close_wait
  7. Ubuntu系统iptables规则的查看和清除
  8. Ubuntu 12.04更新源(转)
  9. 转:Linux 目录结构和常用命令
  10. HDFS的xshell及dfsadmin命令