hdu1242-Rescue(宽度搜索)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242

Rescue

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


 

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...
..#..#.#
#...##..
.#......
........
7 8
#.#####.
#.a#..r.
#..x....
..#..#.#
#...##..
.#......
........
4 5
#####
#a#r#
##xx#
....#

Sample Output

13
7
Poor ANGEL has to stay in the prison all his life.
 

题目大意:

        在一个M*N的监狱中,'r'代表当前角色,'a'代表angel,'x'代表警卫,'.'代表通路,'#'代表墙壁,(N, M <= 200),角色遇到警卫将警卫打败需要花费一步时间且认定角色总能打败警卫,其余通路都将花费一步时间,当角色走到angel的格子中表示角色拯救了angel,计算拯救角色的最小步长。

ac代码:

#include 
#include 
#include 
using namespace std;

const int M = 205; // 行
const int N = 205; // 列

const char R = 'r';
const char WALL = '#';
const char SPACE = '.';
const char X = 'x';
const char A = 'a';

const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};

char g_map[M][N];
int g_mask[M][N]; // 标记是否走过该位置,避免产生路径环路

typedef struct _Node
{
	int x;
	int y;
	int step;
	bool stop; // 标记遇到警卫时的停顿一步
} Node;

queue g_que;
Node g_nodeStart;

void init()
{
	memset(g_map, '#', sizeof(g_map));
	memset(g_mask, 0, sizeof(g_mask));
	while (!g_que.empty()) g_que.pop();
}

int calcSteps()
{
	g_que.push(g_nodeStart);
	Node node;
	while (!g_que.empty())
	{
		node = g_que.front();
		g_que.pop();
		g_mask[node.x][node.y] = 1;
		if (g_map[node.x][node.y] == A) return node.step;
		if (g_map[node.x][node.y] == X && node.stop == false)
		{
			node.step++;
			node.stop = true;
			g_que.push(node);
		}
		else 
		{
			int i;
			for (i = 0; i < sizeof(dx) / sizeof(int); i++)
			{
				Node tempNode;
				tempNode.x = node.x + dx[i];
				tempNode.y = node.y + dy[i];
				tempNode.step = node.step + 1;
				tempNode.stop = false;
				if (g_map[tempNode.x][tempNode.y] != WALL && g_mask[tempNode.x][tempNode.y] == 0) g_que.push(tempNode);
			}
		}
	}
	return 0;
}

int main()
{
	int m, n;
	while (scanf(" %d %d", &m, &n) != EOF)
	{
		init();
		int i, j;
		for (i = 1; i <= m; i++)
		{
			for (j = 1; j <= n; j++)
			{
				scanf(" %1c", &g_map[i][j]);
				if (g_map[i][j] == R)
				{
					g_nodeStart.x = i;
					g_nodeStart.y = j;
					g_nodeStart.step = 0;
					g_nodeStart.stop = false;
				}
			}
		}
		// for (i = 0; i <= m + 1; i++)
		// {
		// 	for (j = 0; j <= n + 1; j++)
		// 	{
		// 		printf("%c", g_map[i][j]);
		// 	}
		// 	printf("\n");
		// }
		int ans = calcSteps();
		if (ans == 0) 
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		else 
			printf("%d\n", ans);
	}
	return 0;
}

 

你可能感兴趣的:(ACM-宽度搜索)