CSU 1815: Enterprising Escape (BFS搜索)

来源:互联网 发布:古装网络剧自制剧男同 编辑:程序博客网 时间:2024/05/21 20:09

1815: Enterprising Escape

    Time Limit: 20 Sec    Memory Limit: 1024 Mb    Submitted: 365    Solved: 117    

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;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩蛋蛋大小不一样有积液怎么办 对派出所的笔录不服怎么办 蛋蛋让尿淹了发红有小红瘩达怎么办 手被皮筋弹肿了怎么办 手被皮筋勒肿了怎么办 皮筋把手挤肿了怎么办 猫被皮筋绑久肿了怎么办 抗链0高关节疼怎么办 近视800度老了怎么办 军检体重不达标怎么办 到交房租没有钱怎么办 房租没到期房东要收回怎么办 客户指定发快递我要怎么办? 跨境汇款被退回怎么办 汇款途径写错了怎么办 快递被菜鸟驿站退回怎么办 电脑登录账户已锁定怎么办 被外管局查到境外汇款买房怎么办 军校生复检被刷怎么办 企业私刻章拿去挂项目怎么办? 中通快递被退回怎么办 网易邮箱提示被修复怎么办 小孩屁股烫红了怎么办 8岁近视400度怎么办 部队体能差的人怎么办 上环5天同房了怎么办 肾结石有3mm了怎么办 4*3mm肾结石好痛怎么办 做完肾结石积水后迟续发烧怎么办 血糖高有肾结石反复发高烧怎么办 肾里面有小结石怎么办 大于2厘米的结石怎么办 双肾结石肾盏扩张怎么办 边防消防警卫部队改革义务兵怎么办 汽车年检尾气复检不合格怎么办 车辆年检尾气不合格复检怎么办? 在瓜子上买车复检有问题怎么办 更换车壳车架号怎么办 吸完甲醛的绿萝怎么办 如果公务员复检不合格有异议怎么办 国考公务员政审没有毕业证怎么办