1702: 我要上蓝翔
时间限制: 1 Sec 内存限制: 128 MB提交: 88 解决: 31
[ 提交][ 状态][ 讨论版]
题目描述
废话不多说,我要上蓝翔。
‘s’ 表示起始所在位置。
‘# ’表示此处为一座山。为了节省体力,不从此处通行。
从‘A’-‘Z’表示各地的经济水平,对应1-26,路过对应字符的地区需要交对应的生活费。
‘l’表示蓝翔技校的所在地。
s 与 l 均为小写字母。
问最少消费是多少
输入
输入包括多组测试数据,每组两个数 n, m; ( 0< n , m <= 100 ) 表示一个h行m列的二维地图。
接下来n行每行m 个字符。
输出
如果不能到达,输出 -1。
样例输入
3 5#sVGFA##ZAlCDBC
样例输出
48
AC-code:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int m,n,s,ans,vis[105][105];
char map[105][105];
struct node
{
int x,y,l;
friend bool operator<(node a,node b)
{
return a.l>b.l;
}
}p,f;
void bfs(int x,int y)
{
int i,ex,ey;
priority_queue<node>q;
memset(vis,0,sizeof(vis));
vis[x][y]=1;
p.x=x,p.y=y,p.l=0;
q.push(p);
while(!q.empty())
{
p=q.top();
q.pop();
for(i=0;i<4;i++)
{
ex=p.x+dx[i];
ey=p.y+dy[i];
if(map[ex][ey]=='l')
{
printf("%d\n",p.l);
return ;
}
if(ex>=0&&ex<n&&ey>=0&&ey<m&&!vis[ex][ey]&&map[ex][ey]!='#')
{
f.x=ex,f.y=ey;
f.l=p.l+(map[ex][ey]-'A'+1);
q.push(f);
vis[ex][ey]=1;
}
}
}
printf("-1\n");
}
int main()
{
int i,j,sx,sy;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<n;i++)
{
scanf("%s",&map[i]);
for(j=0;j<m;j++)
if(map[i][j]=='s')
{
sx=i;
sy=j;
}
}
bfs(sx,sy);
}
return 0;
}