WAJUEJI which home strong!
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
2
-
描述
-
在你的帮助下,弟弟踏上了去蓝翔的路。
那么问题就来了。
-
输入
-
第一个数T,T组测试数据。
两个数 n, m; ( 0< n , m <= 100 ) 表示一个h行m列的二维地图。
接下来n行每行m 个字符。
‘s’ 表示弟弟目前所在位置。
‘# ’表示此处为一座山。为了节省体力,不从此处通行。
从‘A’-‘Z’表示各地的经济水平,对应1-26,路过对应字符的地区需要交对应的生活费。
‘l’表示蓝翔技校的所在地。
s 与 l 均为小写字母。
弟弟只能走四个方向。
输出
-
输出一个数表示弟弟到达蓝翔需要的生活费最小是多少。
如果不能到达,输出 -1。
样例输入
-
3 3 5 #sVGF A##ZA lCDBC 3 3 sAB ABS ABl 3 3 s#B ### ABl
样例输出
-
48 4 -1
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<queue>
using namespace std;
int n,m,vis[205][205];
//vis数组不仅用来记录是否访问过,还记录走到当前位置的最少时间
int ax,ay,bx,by;
char map[105][105];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
struct node
{
int x,y,time;
friend bool operator<(node a,node b)
{
return a.time>b.time; // <为从大到小排列,>为从小到大排列
}
};
int bfs()
{
priority_queue<node>q;
node now,next;
now.x=ax,now.y=ay,now.time=0;
q.push(now);
vis[ax][ay]=1;
while(!q.empty())
{
now=q.top();
q.pop();
for(int i=0;i<4;i++)
{
next=now;
next.x+=dir[i][0];
next.y+=dir[i][1];
if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&map[next.x][next.y]!='#'&&!vis[next.x][next.y])
{
if(isupper(map[next.x][next.y]))//是大写英文字母
{
next.time+=map[next.x][next.y]-'A'+1;
vis[next.x][next.y]=1;
//printf("%d%d:%c_%d\n",next.x,next.y,map[next.x][next.y],next.time);
q.push(next);
}
else if(next.x==bx&&next.y==by)
return next.time;
}
}
}
return 0;
}
int main()
{
int t;
char temp;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%s",map[i]);
for(int j=0;j<m;j++)
{
if(map[i][j]=='s')
ax=i,ay=j;
if(map[i][j]=='l')
bx=i,by=j;
}
}
memset(vis,0,sizeof(vis));
int ans=0;
ans=bfs();
if(ans)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}