• A


        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=1005;
    int n,c,r;
    char cc;
    int cost;
    char a[maxn][maxn];
    int vis[maxn][maxn];
    int dir[][2]={ {1,0},{0,1},{-1,0},{0,-1} };
    
    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<0||x>=r||y<0||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]=1;//清空标记数组
        q.push(Node{x1,y1,0});//给x/y/step赋初值并且插入队列
    
        while(!q.empty())
        {
            Node u=q.top();//另添结构体节点u 取队首值
            q.pop();//弹出队首
            if(u.x<=0||u.x>=r-1||u.y<=0||u.y>=c-1)//达到条件
                return u.step;
                
            for(int i=0;i<4;i++)                 //遍历四个方向
            {
                int x=u.x+dir[i][0];
                int y=u.y+dir[i][1];
                
                if(check(x,y))                   //检查边界符合
                {
                    vis[x][y]=1;                 //标记访问
                    q.push(Node{x, y, u.step+mp[a[x][y]]}); //插入新的横纵节点,步数每次增加数值为map的键值
                }
            }
        }
        return 0;
    }
    
    int main()
    {
        int t;
        int x1,y1;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d%d",&n,&c,&r);
            mp.clear();//注意!!
            for(int i=0;i<n;i++)
            {
                getchar();//注意!!
                scanf("%c %d",&cc,&cost);
                 mp[cc]=cost;
            }
            for(int i=0;i<r;i++)
                scanf("%s",&a[i]);
            for(int i=0;i<r;i++)
            {
                for(int j=0;j<c;j++)
                {
                    if(a[i][j]=='E')
                    {
                        x1=i;
                        y1=j;
                        break;
                    }
                }
            }
            memset(vis,0,sizeof(vis));
            printf("%d
    ",dfs(x1,y1));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    powerdesigner简单使用
    linux进程间通信方式
    linux中fork()函数详解(原创!!实例讲解)
    platform_device与platform_driver
    当心不静的时候
    linux移植简介[MS2]
    使用maven的tomcat:run进行web项目热部署
    SpringMVC &amp; Struts2
    开放产品开发(OPD):OPD框架
    【Android个人理解(八)】跨应用调用不同组件的方法
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7404858.html
Copyright © 2020-2023  润新知