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 ;
}

A - Enterprising Escape 【BFS+优先队列+map】的更多相关文章

  1. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  2. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  3. BFS+优先队列+状态压缩DP+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  4. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  5. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  6. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  7. hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)

    题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...

  8. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  9. D. Lunar New Year and a Wander bfs+优先队列

    D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...

随机推荐

  1. 关于逻辑运算符&&和||及!

    && 表示and ,|| 表示or,!表示not. And(&&):对两个Boolean表达式执行逻辑和. AndAlso(&):与AndAlso类似,关键差异 ...

  2. 洛谷 P2801 教主的魔法 解题报告

    P2801 教主的魔法 题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.--.N. ...

  3. 如何加快JavaScript的加载与执行

    JS 有个很无语的阻塞特性,就是当浏览器在执行JS 代码时,不能同时做其他任何事情,无论其代码是内嵌的还是外部的. 浏览器在碰到一个引入外部JS 文件的<script>标签时会停下所有工作 ...

  4. 一些奇怪的JavaScript试题

    JavaScript有很多地方和我们熟知的C.Java等的编程习惯不同,这些不同会产生很多让人意想不到的事情.前段时间在知乎有人发了写Javascrtip试题,觉得挺好玩的,这里跟大家分享一下. 01 ...

  5. Spring事务只对运行时异常回滚

    我们在使用Spring时候一般都知道事务在遇到异常的时候会回滚,岂不知Spring的事务默认只有在发生运行时异常即:RunTimeException时才会发生事务,如果一个方法抛出Exception或 ...

  6. Intellij Idea debug 远程部署的的tomcat项目

    web项目部署到tomcat上之后,有时需要打断点单步调试,如果用的是Intellij idea,可以通过如下方法实现: 开启debug端口,启动tomcat 以tomcat7.0.75为例,打开bi ...

  7. LowercaseRoutesMVC ASP.NET MVC routes to lowercase URLs

    About this Project Tired of your MVC application generating mixed-case URLs like http://mysite.com/H ...

  8. ansible 批量修改root密码

    [root@sz_fy_virt_encrypt_33_239 fetch]# cat /opt/passwd.yml - hosts: web vars: path: /home/opsadmin ...

  9. 解决在ios下不能自动播放音频的问题

    在ios下面是不允许自动播放音频这个操作的,不过我们可以使用微信提供的sdk来做到自动播放音乐的功能 wx.ready(()=>{}) 引入微信的sdk,之后在回调函数里面执行主动触发背景音乐播 ...

  10. 【洛谷 P2480】 [SDOI2010]古代猪文(中国剩余定理,Lucas定理)

    题目链接 这题出的有点nb,PKU: Pig Kingdom University , NOIP: National Olympics in Informatic of Pigs... 题意:求\(G ...