ZOJ 1649 Rescue

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649


Rescue


Time Limit: 2 Seconds Memory Limit: 65536 KB

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

大意——天使被抓了,他的朋友们想要去救他。问:给你一个监狱的地图,r表示天使的一个朋友,a表示天使,.表示道路,#表示墙壁,x表示守卫。天使的朋友们只能走道路或者杀掉守卫才能继续走,其他不能走。求出能找到天使的最短时间。如若不能,输出题目要求的信息。

思路——因为r可能有多个,所以问题相当于a找到离他最近的r。那么问题与POJ 2312 Battle City类似,用BFS和优先队列解决,因为杀掉守卫要消耗时间。

复杂度分析——时间复杂度:O(n*m),空间复杂度:O(n*m)

附上AC代码:


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
const double pi = acos(-1.0);
const double e = exp(1.0);
const short MAX = 205;
const short dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
short n, m;
char mat[MAX][MAX];
bool flag[MAX][MAX];
struct point
{
	short x, y;
	int step;
	bool operator < (const point & p) const{
		return step > p.step;
	}
};

int bfs(int x, int y);

int main()
{
	ios::sync_with_stdio(false);
	while (cin >> n >> m)
	{
		for (int i=0; i<n; i++)
			cin >> mat[i];
		int x, y;
		memset(flag, 0, sizeof(flag));
		for (int i=0; i<n; i++)
			for (int j=0; j<m; j++)
				if (mat[i][j] == 'a')
				{
					x = i;
					y = j;
					break;
				}
		flag[x][y] = 1;
		int ans = bfs(x, y);
		if (ans)
			cout << ans << endl;
		else
			cout << "Poor ANGEL has to stay in the prison all his life.\n";
	}
	return 0;
}

int bfs(int x, int y)
{
	priority_queue<point> que;
	point p;
	p.x = x;
	p.y = y;
	p.step = 0;
	que.push(p);
	while (!que.empty())
	{
		p = que.top();
		que.pop();
		x = p.x;
		y = p.y;
		int step = p.step;
		if (mat[x][y] == 'r')
			return step;
		for (int i=0; i<4; ++i)
		{
			p.x = x+dir[i][0];
			p.y = y+dir[i][1];
			p.step = 0;
			if (p.x>=0 && p.x<n && p.y>=0 && p.y<m)
			{
				if (mat[p.x][p.y]=='.' || mat[p.x][p.y]=='r')
					p.step = step+1;
				else if (mat[p.x][p.y]=='x')
					p.step = step+2;
				if (p.step>0 && flag[p.x][p.y]==0)
				{
					flag[p.x][p.y] = 1;
					que.push(p);
				}
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值