NYOJ 1100 WAJUEJI which home strong!

来源:互联网 发布:数据库设计典型实例 编辑:程序博客网 时间:2024/04/26 12:53

WAJUEJI which home strong!

输入
第一个数T,T组测试数据。
两个数 n, m; ( 0< n , m <= 100 ) 表示一个h行m列的二维地图。
接下来n行每行m 个字符。
‘s’ 表示弟弟目前所在位置。
‘# ’表示此处为一座山。为了节省体力,不从此处通行。
从‘A’-‘Z’表示各地的经济水平,对应1-26,路过对应字符的地区需要交对应的生活费。
‘l’表示蓝翔技校的所在地。
s 与 l 均为小写字母。
弟弟只能走四个方向。
输出
输出一个数表示弟弟到达蓝翔需要的生活费最小是多少。
如果不能到达,输出 -1。
样例输入
33 5#sVGFA##ZAlCDBC3 3sABABSABl3 3s#B###ABl
样例输出
484-1

优先队列加广搜


#include<iostream>#include<cstring>#include<queue>using namespace std;const int kk[4][2]={{1,0},{-1,0},{0,-1},{0,1}};char map[105][105];int n,m;struct node{int x,y;int step;friend bool operator < (node a,node b){return a.step>b.step;}};int bfs(int x,int y){node t1,t2,tmp1;t1.x=x;t1.y=y;t1.step=0;priority_queue<node> q;q.push(t1);map[x][y]='#';while(!q.empty()){t2=q.top();q.pop();for(int i=0;i<4;i++){tmp1.x=t2.x+kk[i][0];tmp1.y=t2.y+kk[i][1];if(tmp1.x>=0 && tmp1.x<n && tmp1.y>=0 && tmp1.y<m && map[tmp1.x][tmp1.y]!='#'){if(map[tmp1.x][tmp1.y]=='l'){return t2.step;}else{tmp1.step=t2.step+map[tmp1.x][tmp1.y]-'A'+1;}map[tmp1.x][tmp1.y]='#';q.push(tmp1);}}}return -1;}int main(){int t,nn,mm;cin>>t;while(t--){cin>>n>>m;for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>map[i][j];if(map[i][j]=='s'){nn=i;mm=j;}}}int dd=bfs(nn,mm);cout<<dd<<endl;}return 0;}



0 0