CSU-ACM2017暑假集训比赛2 C
来源:互联网 发布:ue软件查看内码 编辑:程序博客网 时间:2024/05/18 07:08
C - (╯°口°)╯(┴—┴
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
26 3 3A 1B 2C 3D 4F 5G 6ABCFECDBG2 6 3A 100B 1000BBBBBBAAAAEBBBBBBB
Sample Output
2400
题目要求找到逃离克林贡人战舰包围的最短时间,摧毁不同字母所代表的不同克林贡战舰所需时间由输入所给定。于是使用一个map来对应摧毁不同战舰所需的时间,以这个时间为关键值,规定时间较短的具有较高优先级,压入优先队列,进行BFS即可得出最短时间。
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <queue>#include <cstring>#include <map>using namespace std;int T, k, w, h, escapeTime, startX, startY;map<char,int> duration; //使用map保存每个字符所对应的权重struct node{ int x, y, step; node(){} node(int _x, int _y, int _step):x(_x), y(_y), step(_step){} bool operator < (const node &a)const{ return step > a.step; }};char grid[1000][1001];bool isAccessed[1004][1004];int dir[][2] = { {1,0},{-1,0},{0,1},{0,-1} };int bfs(){ priority_queue<node> q; //优先队列保存访问顺序,权重较小的优先级高 q.push(node(startX,startY,0)); isAccessed[startX][startY] = true; while(!q.empty()){ node temp = q.top(); q.pop(); if(temp.x == 0 || temp.y == 0 || temp.x == h-1 || temp.y == w-1) return temp.step; for(int i = 0; i < 4; i++){ int vx = temp.x + dir[i][0], vy = temp.y + dir[i][1], nextStep; if(vx >= 0 && vx < h && vy >= 0 && vy < w && !isAccessed[vx][vy]){ nextStep = duration[grid[vx][vy]]; isAccessed[vx][vy] = true; q.push(node(vx,vy,temp.step+nextStep)); } } }}int main(){#ifdef TESTfreopen("test.txt", "r", stdin);#endif // TEST cin >> T; while(T--){ memset(isAccessed, false, sizeof(isAccessed)); memset(grid, 0, sizeof(grid)); duration.clear(); escapeTime = 0; cin >> k >> w >> h; char klingonClass; int dur; for(int i = 0; i < k; i++){ cin >> klingonClass >> dur; duration[klingonClass] = dur; } for(int i = 0; i < h; i++) scanf("%s", grid[i]); for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) if(grid[i][j] == 'E') startX = i, startY = j; cout << bfs() << endl; } return 0;}
阅读全文
0 0
- CSU-ACM2017暑假集训比赛2 C
- CSU-ACM2017暑假集训比赛1 C
- CSU-ACM2017暑假集训比赛1 C
- CSU-ACM2017暑假集训比赛1 C
- CSU-ACM2017暑假集训比赛7 C
- CSU-ACM2017暑假集训比赛2 D
- CSU-ACM2017暑假集训比赛2 B
- CSU-ACM2017暑假集训比赛2 A
- CSU-ACM2017暑假集训比赛2 E
- CSU-ACM2017暑假集训比赛2 HDU
- CSU-ACM2017暑假集训比赛2 CodeForces
- CSU-ACM2017暑假集训比赛1 B
- CSU-ACM2017暑假集训比赛1 A
- CSU-ACM2017暑假集训比赛3D
- CSU-ACM2017暑假集训比赛7 A
- CSU-ACM2017暑假集训比赛7 B
- CSU-ACM2017暑假集训比赛7
- CSU-ACM2017暑假集训比赛7
- iOS 如何判断一个点在圆、方框、三角形区域内?
- 多媒体开发---视频播放器开发(android)
- C++一维数组
- JSON解析
- mac 下 Apk反编译
- CSU-ACM2017暑假集训比赛2 C
- 定位fixed,使得可点击元素不可点击问题解决
- NYOJ 92图像有用区域
- Codeforces Round #427 (Div. 2)
- HDU 6034 字符贪心赋值计算问题
- CodeForces
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
- 巧用CheckedTextView完成自定义radiobutton的listview
- git revert 与 git reset