A - Enterprising Escape 【BFS+优先队列+map】
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】的更多相关文章
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- BFS+优先队列+状态压缩DP+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others) Memo ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- D. Lunar New Year and a Wander bfs+优先队列
D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...
随机推荐
- sqoop将oracle数据导入hdfs集群
使用sqoop将oracle数据导入hdfs集群 集群环境: hadoop1.0.0 hbase0.92.1 zookeeper3.4.3 hive0.8.1 sqoop-1.4.1-incubati ...
- Visual Studio调试之断点技巧篇
原文链接地址:http://blog.csdn.net/Donjuan/article/details/4618717 函数断点 在前面的文章Visual Studio调试之避免单步跟踪调试模式里面我 ...
- Visual Studio调试之断点技巧篇补遗
原文链接地址:http://blog.csdn.net/Donjuan/article/details/4649372 讲完Visual Studio调试之断点技巧篇以后,翻翻以前看得一些资料和自己写 ...
- 洛谷 P1829 [国家集训队]Crash的数字表格 / JZPTAB 解题报告
[国家集训队]Crash的数字表格 / JZPTAB 题意 求\(\sum\limits_{i=1}^n\sum\limits_{j=1}^mlcm(i,j)\),\(n,m\le 10^7\) 鉴于 ...
- JS格式化时间(支持小程序,兼容IOS)
})-(\d{})-(\d{})T(\d{}):(\d{}):(\d{})/ /** * @function format time * @param val, format * @return {s ...
- HDU1213:How Many Tables(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- IDEA 使用maven创建web项目,打包war时不会创建class文件
使用maven创建项目后我有创建了个src的目录,导致maven编译不能识别我创建的src文件下的Java文件 修改这样后就可以识别编译Java文件 今天又给自己挖了个坑.......
- IDEA2017 使用(二)
1.鼠标悬浮在方法上显示api 2.关闭拼写检查 3.自动导入包(存在多个包时需要手动导入) 4.设置方法线
- How to setup Active Directory (AD) In Windows Server 2016
Windows Server 2016 is the newest server operating system released by Microsoft in October 12th, 201 ...
- vue文件使用stylus报错问题
先npm install stylus --save然后安装你少的page.json中依赖:npm install stylus-loader css-loader style-loader --sa ...