hdu 1242 Rescue_bfs+优先队列

翻出以前的代码看看

题意:走迷宫,遇到敌人要花一分钟。

#include<iostream>
#include<queue>
using namespace std;
char map[205][205];
int N = 999999999;
int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
int n,m;
struct node
{
	int x;
	int y;
	int step;
	friend bool operator<(node a, node b) 
    {  
        return a.step>b.step;  
    }
}begin,end;
int bfs()
{
	priority_queue<node>q;
	q.push(begin);
	node temp,t;
	int i;
	while(!q.empty())
	{
		t=q.top();
		q.pop();
		for(i=0;i<4;i++)
		{
			temp.x=t.x+dir[i][0];
			temp.y=t.y+dir[i][1];
			if(map[temp.x][temp.y]!='#'&&temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m)
			{
				if(map[temp.x][temp.y]=='.')
				{
					map[temp.x][temp.y]='#';
					temp.step=t.step+1;
					q.push(temp);
				}
				else if(map[temp.x][temp.y]=='x')
				{
					map[temp.x][temp.y]='#';
					temp.step=t.step+2;
					q.push(temp);
				}
				else
				{
					return end.step=t.step+1;
				}
			}
		}
	}
	return end.step;
}

					



int main()
{
	int i,j;
	while(cin>>n>>m)
	{
		for(i=0;i<n;i++)
		{
			getchar();
			for(j=0;j<m;j++)
			{
				cin>>map[i][j];
				if(map[i][j]=='a')
				{
					begin.x=i;
					begin.y=j;
					begin.step=0;
					map[i][j]='#';
				}
			}
		}
		end.step=N;
		if(bfs()==N)
			cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
		else
			cout<<end.step<<endl;
	}
	return 0;
}

	




 
  

你可能感兴趣的:(优先队列)