HDU1242 - Rescue - bfs

1.题目描述:

Rescue

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


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
 

Recommend
Eddy
 
2.题意概述:

碰到士兵多发费一秒,问到达目的地最短时间

3.解题思路:

最短时间问题都可以考虑BFS尝试,队列看要求,其实不一定要优先队列,这道题算模板题吧。

4.AC代码:

#include 
#include 
#include 
#define N 202
using namespace std;

struct node
{
	int x, y, step;
	friend bool operator<(node n1, node n2)
	{
		return n2.step < n1.step;
	}
};

int n, m, vis[N][N];
char map[N][N];
int sx, sy, x2, y2;
int dir[4][2] = { 1,0,-1,0,0,1,0,-1 };

int judge(int x, int y)
{
	if (x < 0 || y < 0 || x >= n || y >= m || !vis[x][y] || map[x][y] == '#')
		return 1;
	return 0;
}

int bfs()
{
	priority_queue Q;
	node a, next;
	a.x = sx;
	a.y = sy;
	a.step = 0;
	Q.push(a);
	vis[sx][sy] = 0;
	while (!Q.empty())
	{
		a = Q.top();
		Q.pop();
		if (a.x == x2 && a.y == y2)
			return a.step;
		for (int i = 0; i < 4; i++)
		{
			next = a;
			next.x += dir[i][0];
			next.y += dir[i][1];
			if (judge(next.x, next.y))//判断
				continue;
			next.step++;
			if (map[next.x][next.y] == 'x')//卫兵处多花费了一秒
				next.step++;
			if (vis[next.x][next.y] >= next.step)//存入最小时间
			{
				vis[next.x][next.y] = next.step;
				Q.push(next);
			}
		}
	}
	return 0;
}

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

	return 0;
}

你可能感兴趣的:(acm,算法,hdu,BFS)