zoj1649-Rescue(priority_queue)

#include<iostream>
#include<queue>
using namespace std;
char maze[201][201];//迷宫
const int pmove[4][2]={{0,1},{0,-1},{-1,0},{1,0}};//移动
int n;//多少行
int m;//多少列
int sx,sy,ex,ey;
int prison();
struct Node
{
	friend bool operator < (Node a,Node b)
	{
		return a.step>b.step;
	}
	int x,y,step;
};
int main()
{
	int i,j,result;
	while(cin>>n>>m)
	{
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			cin>>maze[i][j];
			if(maze[i][j]=='r')
			{
				sx=j;
				sy=i;
				maze[i][j]='#';
				continue;
			}
			if(maze[i][j]=='a')
			{
				ex=j;
				ey=i;
				continue;
			}
		}
		result=prison();
		if(result)
			cout<<result<<endl;
		else
			cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
	}
	return 0;
}
int prison()
{
	priority_queue<Node> que;
	Node now,temp;
	int v;
	now.x=sx;
	now.y=sy;
	now.step=0;
	que.push(now);
	while(!que.empty())
	{
		temp=que.top();
		que.pop();
		for(v=0;v<4;v++)
		{
			now.x=temp.x+pmove[v][0];
			now.y=temp.y+pmove[v][1];
			if(now.x==ex&&now.y==ey)
				return temp.step+1;
			if(now.x<0||now.y<0||now.x>m-1||now.y>n-1)
				continue;
			if(maze[now.y][now.x]!='#')
			{
				if(maze[now.y][now.x]=='.')
					now.step=temp.step+1;
				else
					now.step=temp.step+2;
				que.push(now);
				maze[now.y][now.x]='#';
			}
		}
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值