xws117的专栏

新浪微博: http://weibo.com/2139773423/

hdu 1242 (bfs+优先队列)
# include <iostream>
# include <queue>
# include <cstring>
using namespace std;
int m,n;
char map[205][205];
int visit[205][205];
int xx[4]={1,-1,0,0};
int yy[4]={0,0,1,-1};
struct point
{
	int x;
	int y;
	int step;
	friend bool operator < (point a,point b)
	{
		return a.step>b.step;
	}
}a,b;
int bfs()
{
	int flag=0;
	memset(visit,0,sizeof(visit));
	visit[a.x][a.y]=1;
	priority_queue<point> q;
	q.push(a);
	while(!q.empty())
	{
		b=q.top();
		q.pop();
		int i;
		if(map[b.x][b.y] == 'r')  
        {  
            flag = b.step;  
            return flag;  
        }     
		for(i=0;i<4;i++)
		{
			a.x=b.x+xx[i];
			a.y=b.y+yy[i];
			a.step=b.step+1;
			if(a.x<0||a.x>=m||a.y<0||a.y>=n)
				continue;
			if(!visit[a.x][a.y]&&map[a.x][a.y]!='#')
			{
				visit[a.x][a.y]=1;
				if(map[a.x][a.y]=='x')
					a.step++;
				q.push(a);
				
			}
		}
	}
	return 0;
}
int main()
{
	while(scanf("%d %d",&m,&n)!=EOF)
	{
		int i,j;
		for(i=0;i<m;i++)
		{
			scanf("%s",map[i]);
			for(j=0;j<n;j++)
			{
				if(map[i][j]=='a')
				{
					a.x=i;
					a.y=j;
					a.step=0;
				}
			}
		}   
		int step = bfs();
		if(step)
			printf("%d\n",step);
		else
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		
	}
	return 0;
}
记住优先队列的使用
阅读更多
个人分类: hdu 搜索
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

hdu 1242 (bfs+优先队列)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭