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;}
原创粉丝点击