CSU-ACM2017暑假集训比赛2 C - (╯°口°)╯(┴—┴

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

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

题目要求找到逃离克林贡人战舰包围的最短时间,摧毁不同字母所代表的不同克林贡战舰所需时间由输入所给定。于是使用一个map来对应摧毁不同战舰所需的时间,以这个时间为关键值,规定时间较短的具有较高优先级,压入优先队列,进行BFS即可得出最短时间。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 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 TEST
freopen("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;
}

你可能感兴趣的:(CSU-ACM2017暑期训练,BFS,C++,BFS)