HDU1242 Rescue

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<memory.h>
#include<queue>
#include<stack>

using namespace std;

int map[211][211];
int n,m;
int x_s,y_s;
int x_e,y_e;

struct node
{
	int x,y;
	int step;
	friend bool operator<(node n1,node n2)
	{
		return n2.step<n1.step;
	}
};
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};

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

int BFS()
{
	priority_queue<node>q;
	node cur,next;
	int i;


	cur.x=x_s;
	cur.y=y_s;
	cur.step=0;
	map[cur.x][cur.y]=1;
	q.push(cur);
	while(!q.empty())
	{
		cur=q.top();
		q.pop();
		if(cur.x==x_e && cur.y==y_e)	return cur.step;
		for(i=0;i<4;i++)
		{
			next.x=cur.x+dir[i][0];
			next.y=cur.y+dir[i][1];


			if(judge(next.x,next.y))	continue;
			if(map[next.x][next.y]==-1)	next.step=cur.step+2;
			else						next.step=cur.step+1;
			map[next.x][next.y]=1;
			q.push(next);
		}
	}
	return -1;
}

int main()
{
	int ans;
	int i,l;
	char str[211];
	
	while(scanf("%d%d",&n,&m)!=-1)
	{
		memset(map,0,sizeof(map));


		for(i=0;i<n;i++)
		{
			scanf("%s",str);
			for(l=0;str[l];l++)
			{
				if(str[l]=='r')		{x_s=i;y_s=l;}
				else if(str[l]=='a'){x_e=i;y_e=l;}
				else if(str[l]=='#')map[i][l]=1;
				else if(str[l]=='.')map[i][l]=0;
				else if(str[l]=='x')map[i][l]=-1;
			}
		}
		ans=BFS();
		if(ans==-1)	printf("Poor ANGEL has to stay in the prison all his life.\n");
		else		printf("%d\n",ans);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值