ZOJ-1649 Rescue

2 篇文章 0 订阅
1 篇文章 0 订阅
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

点击打开题目链接


分析:如果这道题中没有警卫,那么只需用普通队列来广搜,因为任意两可行点之间的权值(耗时)都是1,因此第一次到达目标位置时的耗时即使最短时间;但是因为题中要求干掉警卫要额外耗时,即权值为2,此时若再用普通队列,那么它不一定会按照最短耗时的顺序出队,这样一来当第一次到达终点时,其耗时就不一定是最短时间了。所以我们可采用优先队列,保证它使耗时短的结点先出队,这样当第一次来到终点时,该方案是最优路线。

代码如下。

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>

using namespace std;

struct PosNode
{
	int x, y;	//x是行,y是列
	int times;	//花费时间
	
	bool operator < (const PosNode &other) const{
		return times > other.times;
	}
}Angle, Friend, s, t;

char map[202][202];
int vis[202][202];
int N, M;

const int dir[][2] = { {1,0},{-1,0},{0,1},{0,-1} };

int bfs()
{
	priority_queue<PosNode>q;
	memset(vis, 0, sizeof(vis));

	q.push(Friend);
	vis[Friend.x][Friend.y] = 1;
	while (!q.empty())
	{
		s = q.top(); q.pop();
		if (s.x == Angle.x&&s.y == Angle.y)	//到达天使位置,返回最短时间
			return s.times;
		for (int i = 0; i < 4; i++)		//扩展
		{
			int xx = s.x + dir[i][0];
			int yy = s.y + dir[i][1];
			if (xx >= 0 && xx < N&&yy >= 0 && yy < M&& map[xx][yy] != '#'&&!vis[xx][yy])
			{
				t.x = xx; t.y = yy; t.times = s.times + 1;
				if (map[xx][yy] == 'x')	//是警卫,耗时再加1
					t.times += 1;
				q.push(t);
				vis[xx][yy] = 1;
			}
		}
	}
	return -1;	//失败
}

int main()
{
	while (scanf("%d %d", &N, &M) != EOF)
	{
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < M; j++)
			{
				scanf(" %c", &map[i][j]);
				if (map[i][j] == 'a') { Angle.x = i; Angle.y = j; Angle.times = 0; };	//记录天使位置
				if (map[i][j] == 'r') { Friend.x = i; Friend.y = j; Friend.times = 0; };//记录友军位置
			}
		}

		int ans = bfs();
		if (ans < 0)
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		else
			printf("%d\n", ans);
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值