【HDOJ 1242 Rescue】bfs+优先队列




#include<iostream>
using namespace std;
#include<queue>

int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
char maze[201][201];
int n,m;

struct Node
{
	int x,y,t;
	Node(int xx,int yy,int tt);
	bool operator < (Node b) const
	{
		return b.t<t;
	}
};

Node::Node(int xx,int yy,int tt)
{
	x=xx;
	y=yy;
	t=tt;
}

void bfs(int sx,int sy)
{
	priority_queue<Node> pq;
	Node temp(sx,sy,0);
	pq.push(temp);
	while(pq.size())
	{
		Node top=pq.top();
		pq.pop();
		maze[top.x][top.y]='#';
		for(int i=0;i<4;i++)
		{
			temp.x=top.x+dir[i][0];
			temp.y=top.y+dir[i][1];
			if(temp.x<0||temp.y<0||temp.x>=n||temp.y>=m)
			{
				continue;
			}
			if(maze[temp.x][temp.y]=='#')
			{
				continue;
			}
			if(maze[temp.x][temp.y]=='r')
			{
				cout<<top.t+1<<endl;
				return;
			}
			if(maze[temp.x][temp.y]=='.')
			{
				temp.t=top.t+1;
			}
			if(maze[temp.x][temp.y]=='x')
			{
				temp.t=top.t+2;
			}
			pq.push(temp);
		}
	}
	cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}

int main(void)
{
	while(~scanf("%d %d",&n,&m))
	{
		int sx,sy;
		for(int i=0;i<n;i++)
		{
			for(int k=0;k<m;k++)
			{
				cin>>maze[i][k];
				if(maze[i][k]=='a')
				{
					sx=i;
					sy=k;
				}
			}
		}
		bfs(sx,sy);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值