hdu 1242 (bfs+优先队列)

原创 2013年12月06日 00:36:37
# 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 1242 Rescue(优先队列 && 广搜BFS)

Rescue 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:Angel被关押在牢里,Angel的朋友要去救Angel,给出Angel的朋...

hdu 1242 Rescue bfs+优先队列 解题报告

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub...

Rescue HDU1242 (BFS+优先队列)

HDU1242

HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is...

hdu1242 Rescue(优先队列bfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:#为墙,.为路,r为其朋友,a天使,x为怪物,每打一个怪物消耗1时间,求能够救到公主的最短时间...

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The...

HDU 1242 Rescue(BFS+优先队列)

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub...

【搜索之BFS + 优先队列】杭电 hdu 1242 Rescue

/* THE PROGRAM IS MADE BY PYY */ /*-----------------------------------------------------------------...

ZOJ 1649 && HDU 1242 Rescue (BFS + 优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M...

Hdu 1242 & Zoj 1649 Rescue (优先队列+BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:朋友(r)杀敌人(x)救天使(a),时间最短型走迷宫,走格子耗时1s,杀敌人额外耗时1s...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1242 (bfs+优先队列)
举报原因:
原因补充:

(最多只允许输入30个字)