CSU 1815: Enterprising Escape (BFS搜索)
来源:互联网 发布:古装网络剧自制剧男同 编辑:程序博客网 时间:2024/05/21 20:09
1815: Enterprising Escape
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 toescape.
Sample Input
26 3 3A 1B 2C 3D 4F 5G 6ABCFECDBG2 6 3A 100B 1000BBBBBBAAAAEBBBBBBB
Sample Output
2400
Hint
Source
2013 Pacific Northwest Region Programming Contest
简单的BFS搜索题,处理每个字母代表的数值,可以用数组将其转化成数字来处理,也可以直接用map来存储每个字母所代表的的信息
#include <iostream>#include <cstring>#include <stack>#include <cstdio>#include <cmath>#include <queue>#include <algorithm>#include <vector>#include <set>#include <map>const double eps=1e-8;const double PI=acos(-1.0);using namespace std;struct st{ int x; int y; int step; friend bool operator <(st a,st b){ return a.step>b.step;}}node;char a[1005][1005];int v[1005][1005];int d[][2]={{1,0},{0,1},{0,-1},{-1,0}};int k,n,m,num; map<char,int> s;int bfs(int x,int y){ priority_queue<st>q; node.x=x; node.y=y; node.step=0; q.push(node); v[x][y]=1; while(!q.empty()) { st tem=q.top(); //cout<<tem.x<<" "<<tem.y<<endl; q.pop(); st tp; if(tem.x>=m-1||tem.y>=n-1||tem.x<=0||tem.y<=0) { return tem.step;//cout<<"t:"<<e<<endl; } for(int i=0; i<4; i++) { tp.x=tem.x+d[i][0]; tp.y=tem.y+d[i][1]; if(v[tp.x][tp.y]) continue; tp.step=tem.step+s[a[tp.x][tp.y]]; q.push(tp); v[tp.x][tp.y]=1; //cout<<tp<<endl; } } return -1;}int main(){ int t; scanf("%d",&t); while(t--){ memset(v,0,sizeof(v)); char c; scanf("%d%d%d",&k,&n,&m); getchar(); for(int i=0;i<k;i++){ scanf("%c %d",&c,&num); getchar(); s[c]=num; } int x,y; for(int i=0;i<m;i++) scanf("%s",a[i]); for(int i=0;i<m;i++) for(int j=0;j<n;j++) if(a[i][j]=='E'){ x=i;y=j; } printf("%d\n",bfs(x,y)); } return 0;}
- CSU 1815: Enterprising Escape (BFS搜索)
- 文章标题 CSU 1815 : Enterprising Escape(BFS--优先队列)
- CSU 1815:Enterprising Escape(BFS+优先队列)
- Enterprising Escape
- Enterprising Escape
- hnu 12921 Enterprising Escape
- 【搜索】 HDU 3533 Escape BFS 预处理
- FZU 2196 Escape(BFS预处理+BFS搜索)
- 2017湖南多校第一场-Enterprising Escape
- CSU 1908: The Big Escape
- BFS-hdu-3533-Escape
- HDOJ 2364 Escape (bfs)
- hdoj 2364 Escape(BFS)
- HDU-3533-Escape(BFS)
- HDU3533:Escape(BFS)
- hdu Escape(bfs+模拟)
- FZU 2196 Escape BFS
- FZUOJ2196 Escape(bfs)
- 设计模式六大原则之里氏替换原则
- 解决远程连接MYSQL提示Host is not allowed to connect to this MySQL server
- 文章标题
- VS2015链接mysql数据库
- 关于前后端分离开发的方法,jQuery的Mockjax:Ajax请求模拟
- CSU 1815: Enterprising Escape (BFS搜索)
- 每天五分钟linux(14)-head
- Asp.net 远程操作服务端串口
- 两个文件合并成一个二进制文件,再将这两个文件还原
- java 泛型 学习笔记
- POJ
- <asp:Button>先执行前端js函数,再执行后台的事件处理
- 工作一年之后,那些。。。
- request、session、application(ServletContext)