Rescue hdu

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct pos
{
	int count,x,y;
	friend bool operator<(pos a,pos b)
	{
		return a.count>b.count;//从小到大排列 
	}
};
int dir[4][2]={0,1,0,-1,1,0,-1,0};
char arr[205][205];
int vis[205][205],n,m;
void bfs(int x,int y)
{
	priority_queue<pos>qu;
	pos cur,nex;
	cur.x=x;
	cur.y=y;
	cur.count=0;
	qu.push(cur);
	while(!qu.empty())
	{
		cur=qu.top();
		qu.pop();
		if(arr[cur.x][cur.y]=='r')
		{
			cout<<cur.count<<endl;
			return;
		}
		for(int i=0;i<4;i++)
		{
			nex.x=cur.x+dir[i][0];
			nex.y=cur.y+dir[i][1];
			if(!vis[nex.x][nex.y]&&arr[nex.x][nex.y]!='#'&&nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m)
			{
				vis[nex.x][nex.y]=1;
				if(arr[nex.x][nex.y]=='x')
				nex.count=cur.count+2;
				else 
				nex.count=cur.count+1;
				qu.push(nex);
			}
		}
	}
	cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
	return;
}

int main()
{
	int stx,sty;
	while(scanf("%d%d",&n,&m)==2)
	{
		for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		{
			cin>>arr[i][j];
			if(arr[i][j]=='a')
			{
				stx=i;
				sty=j;
			}
		}
		memset(vis,0,sizeof(vis));
		vis[stx][sty]=1;
		bfs(stx,sty);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值