HDU1242 Rescue BFS+优先队列

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5895    Accepted Submission(s): 2192


Problem Description

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
 

Input

First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

Process to the end of the file.
 

Output

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
 

Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
 

Sample Output
13
 
  一个简单的搜索题,这里因为题目中说了可能有多个营救者,所以从a开始反向搜索,这样便能找到最近的营救者,该题在入队时因为时间并不是严格的+1,所以在一定程度上破坏了BFS的性质,所以加之优先队列进行修正。
  代码如下:
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
using namespace std;

char map[205][205], hash[205][205];

struct Node
{
	int x, y, step;
	bool operator < ( const Node &t ) const
	{
	    return t.step< step;
	}
}info;

int sx, sy, dir[4][2]= { 1, 0, -1, 0, 0, 1, 0, -1 };

bool BFS( int &ans )
{
	memset( hash, 0, sizeof( hash ) );
	priority_queue< Node >q;
	info.x= sx, info.y= sy, info.step= 0;
	hash[sx][sy]= 1;
	q.push( info );
	while( !q.empty() )
	{
		Node pos= q.top();
		q.pop();
		if( map[ pos.x ][ pos.y ]== 'r' )
		{
			ans= pos.step;
			return true;
		}
		for( int i= 0; i< 4; ++i )
		{
			int x= pos.x+ dir[i][0], y= pos.y+ dir[i][1], step= pos.step+ 1;
			if( map[x][y]!= '#'&& map[x][y]!= 0 )
			{
				if( ( map[x][y]== '.'|| map[x][y]== 'r' )&& !hash[x][y] )
				{
					info.x= x, info.y= y, info.step= step;
					hash[x][y]= 1;
					q.push( info );
				}
				else if( map[x][y]== 'x'&& !hash[x][y] )
				{
					info.x= x, info.y= y, info.step= step+ 1;
					hash[x][y]= 1;
					q.push( info );
				}
			}
		}
	}
	return false;
}

int main()
{
	int N, M;
	while( scanf( "%d %d", &N, &M )!= EOF )
	{
		int flag= 0;
		memset( map, 0, sizeof( map ) );
		for( int i= 1; i<= N; ++i )
		{
			scanf( "%s", map[i]+ 1 );
			for( int j= 1; j<= M; ++j )
			{
				if( !flag&& map[i][j]== 'a' )
				{
					sx= i, sy= j;
					flag= 1;
				}
			}
		}
		int ans;
		if( BFS( ans ) )
		{
			printf( "%d\n", ans );
		}
		else
		{
			puts( "Poor ANGEL has to stay in the prison all his life." );
		}
	}
}

原文地址:https://www.cnblogs.com/Lyush/p/2132087.html