HDU 1242 (BFS+优先队列)

这个题目直接BFS+优先队列;没有其他考虑的,从天使开始搜就行啦!!


AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
char q[208][208];
int w[208][208];
int m,n;
struct node{
	int a,b;
	int pri;
	bool operator<(const node &t)const{
		return pri>t.pri;
	}
};
int getes(int a,int b){
	if(a>=1 && a<=n && b >=1 && b <=m && q[a][b] != '#' && w[a][b]==0){
		return 1;
	}
	return 0;
}
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int bfs(int x,int y){
	priority_queue<node> que;
	node in,out;
	int c,d,i;
	in.a=x;in.b=y;
	in.pri=0;
	que.push(in);
	while(que.size() != 0){
		out=que.top();
		que.pop();
		c=out.a;
		d=out.b;
		if(q[c][d] == 'r'){
			return out.pri;
		}
		for(i=0;i<4;i++){
			c=out.a+dir[i][0];
			d=out.b+dir[i][1];
			if(getes(c,d) == 1){
				w[c][d]=1;
				in.a=c;
				in.b=d;
				in.pri=out.pri+1;
				if(q[c][d] == 'x'){
					in.pri+=1;
				}
				que.push(in);
			}
		}
	}
	return 0;
}

int main(){
	int i,j,sum;
	int c,d;
	while(~scanf("%d%d",&n,&m)){
		for(i=1;i<=n;i++){
			for(j=1;j<=m;j++){
				cin>>q[i][j];
				if(q[i][j] == 'a'){
					c=i;
					d=j;
				}
			}
		}
		memset(w,0,sizeof(w));
		w[c][d]=1;
		sum=bfs(c,d);
		if(sum == 0){
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		}
		else{
			printf("%d\n",sum);
		}
	}
	return 0;
}

路途中。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值