hdu1242-Rescue(宽度搜索)

本文解析了一个典型的ACM竞赛问题——救援问题。在M*N的监狱地图中,通过算法寻找从角色到被囚禁的angel的最短路径,考虑墙壁、通路、警卫等元素,特别关注与警卫相遇时的特殊处理。文章提供了详细的算法实现,包括使用队列进行广度优先搜索,并处理遇到警卫时的额外步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接: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 <stdio.h>
#include <memory.h>
#include <queue>
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<Node> 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值