Hduoj1242【广搜+优先队列】

/*Rescue
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16997    Accepted Submission(s): 6151


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

Author
CHEN, Xue

Source
ZOJ Monthly, October 2003 
*/ 
#include
#include
char map[220][220];
int n, m, min, step, dx[4] = {0,0,1,-1}, dy[4] = {1,-1,0,0}, vis[220][220];
int quee[40000][3];
void bfs(int x, int y)
{
	int i, j, nx, ny, front = 0, rear = 0, num;
	quee[rear][0] = x;
	quee[rear][1] = y;
	quee[rear++][2] = 0;
	while(front < rear)
	{
		x = quee[front][0];
		y = quee[front][1];
		num = quee[front++][2];
		for(i = 0; i < 4; ++i)
		{
			nx = x + dx[i];
			ny = y + dy[i];
			if(nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] != '#' )
			{
				int k ;
				if(map[nx][ny] == 'x')
				k = num+2;
				else
				k = num+1;
				if(map[nx][ny] == 'r')
				{
					if(k < min)
					min = k;
				}
				map[nx][ny] = '#';//change wall
				if(front == rear)
				{
					quee[rear][0] = nx;
					quee[rear][1] = ny;
					quee[rear][2] = k;
					rear++;
				}
				else
				{
					for(j = front; j < rear; ++j)//find   k
					{
						if(quee[j][2] > k)
						{
							for(int l = rear; l > j; l--)//move behind
							{
								quee[l][0] = quee[l-1][0];
								quee[l][1] = quee[l-1][1];
								quee[l][2] = quee[l-1][2];
							}
							quee[j][0] = nx;//put in
							quee[j][1] = ny;
							quee[j][2] = k;
							break;
						}
					}
					if(j == rear)//not find
					{
						quee[rear][0] = nx;
						quee[rear][1] = ny;
						quee[rear][2] = k;
					}
					rear++;
				}
			}
		}
		
	}
}
int main()
{
	int i, j, k, x, y;
	while(scanf("%d%d", &n, &m) != EOF)
	{
		getchar();
		for(i = 0;  i < n; ++i)
		{
			for(j = 0; j < m; ++j)
			{
				scanf("%c", &map[i][j]);
				if(map[i][j] == 'a')
				{
					x = i;
					y = j;
				}
			}
			getchar();
		}
		min = 100000000;
		memset(quee, 0, sizeof(quee));
		bfs(x,y);
		if(min == 100000000)
		printf("Poor ANGEL has to stay in the prison all his life.\n");
		else
		printf("%d\n", min);
	}
	return 0;
}


题意:给一个迷宫,求起点到终点的时间,如果遇到警卫单位时间加1,输出最小的时间,若不能到达 则输出。。。。

思路:用广搜+有限队列来做,由于有警卫的存在所以要用优先队列来求出每个点到起点的最小距离。


你可能感兴趣的:(搜索)