NYOJ 1100 WAJUEJI which home strong!

24 篇文章 0 订阅
21 篇文章 0 订阅

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。
样例输入
3
3 5
#sVGF
A##ZA
lCDBC
3 3
sAB
ABS
ABl
3 3
s#B
###
ABl
样例输出
48
4
-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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黎轩栀海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值