ZOJ 1649: Rescue(BFS)

本文解析了一道ZJU ACM竞赛题目,讲述了如何通过广度优先搜索算法,计算在包含墙壁、道路和守卫的监狱中,从朋友位置到被囚禁的天使位置所需的最短时间。考虑到杀死守卫需要额外时间,算法巧妙地处理了这一复杂条件。

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

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649

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

题意分析:

一个人被抓进了监狱,现在他的盆友要去救他, 每次只能上下左右移动,每移动一次需要1分钟, 监狱中可能会有守卫, 杀死一个守卫需要1分钟,也就是说想到达一个有守卫的地方需要2分钟,假设能够杀死所有守,求能救人所需要的最短时间。

解题思路:

遍历所有能够走到的格子,需要注意的是每次杀死一个守卫需要的时间更长,所以不直接加入原队列之中,如果直接加入可能后面会先处理这个步数更长的,而忽略了后面加入的只加1步的。

#include <stdio.h>
#include <string.h>
#include <queue>
#define N 220
using namespace std;
int n, m;
char str[N][N];
bool book[N][N];
struct date{
	int x;
	int y;
	int temp;
};
int bfs(int x, int y)
{
	int tx, ty;
	int nex[4][2]={0,1, 0,-1, 1,0, -1,0};
	memset(book, false, sizeof(book));
	queue<date>q, p;
	q.push(date{x, y, 0});
	book[x][y]=true;
	while(!q.empty() || !p.empty())
	{
		date u, v;
		if(q.empty())
		{
			u=p.front();
			p.pop();
		}
		else if(p.empty())
		{
			u=q.front();
			q.pop();
		}
		else
		{
			u=q.front();
			v=p.front();
			if(u.temp>v.temp)
			{
				u=v;
				p.pop();
			}
			else
				q.pop();
		}	
		if(str[u.x][u.y]=='a')
			return u.temp;
			
		for(int i=0; i<4; i++)
		{
			tx=u.x+nex[i][0];
			ty=u.y+nex[i][1];
			if(tx>=n || ty>=m || tx<0 || ty<0)
				continue;
			if(str[tx][ty]=='#')
				continue;
			if(str[tx][ty]=='.' && book[tx][ty]==false)
			{
				book[tx][ty]=true;
				q.push({tx, ty, u.temp+1});	
			}
			if(str[tx][ty]=='x' && book[tx][ty]==false)
			{
				book[tx][ty]=true;
				p.push({tx, ty, u.temp+2});
			}
			if(str[tx][ty]=='a')
				return u.temp+1;
		}
	}
	return -1;
}
int main()
{
	int i, j, ans;
	while(scanf("%d%d", &n, &m)!=EOF)
	{
		ans=-1;
		for(i=0; i<n; i++)
			scanf("%s", str[i]);
		for(i=0; i<n; i++)
			for(j=0; j<m; j++)
				if(str[i][j]=='r')
				{
					ans=bfs(i, j);
					break;
				}
		if(ans==-1)
			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
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值