WAJUEJI which home strong! nyoj 1100

优先队列+BFS; 
//http://acm.nyist.net/JudgeOnline/problem.php?pid=1100
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,vis[102][102];
char map[102][102];
int dx[4]={0,1,0,-1};
int dy[4]={-1,0,1,0};
int mon[102];
struct node{
	int x,y;
	int money;
	friend bool operator< (node n1, node n2){
		return n1.money>n2.money;
	} 
};

int check(int x,int y){
	if(x>=n||(y>=m)||(x<0)||(y<0))return 0;
	if(vis[x][y]==1)return 0;
	if(map[x][y]=='#')return 0;
	return 1;
}


int bfs(int x1,int y1){
	vis[x1][y1]=1;
	priority_queue<node>q;
	struct node p,t;
	p.x=x1;
	p.y=y1;
	p.money=0;
	q.push(p);
	while(!q.empty()){
		t=q.top();
		q.pop();
		int x=t.x,y=t.y;
		for(int i=0;i<4;i++){
	    	int nx=x+dx[i],ny=y+dy[i];
	    	if(check(nx,ny)){
	    		vis[nx][ny]=1;
	     		if(map[nx][ny]=='l'){
	        		return t.money;
	        	}
	    		p.x=nx;p.y=ny;
	    		p.money=t.money+map[nx][ny]-'A'+1;
	    		q.push(p);
	        }
    	}
	}
	return -1;
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		getchar();
		int a,b;
		memset(vis,0,sizeof(vis));
    	for(int i=0;i<n;i++){
	    	for(int j=0;j<m;j++){
	    		scanf("%c",&map[i][j]);
	    		if(map[i][j]=='s'){
	    			a=i;b=j;
	    		}
	    	}
	    	getchar();
    	}
    	printf("%d\n",bfs(a,b));
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值