我是靠谱客的博主 清秀老师,最近开发中收集的这篇文章主要介绍ZOJ-1649 Rescue,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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("%dn", ans);
	}
	return 0;
}




最后

以上就是清秀老师为你收集整理的ZOJ-1649 Rescue的全部内容,希望文章能够帮你解决ZOJ-1649 Rescue所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部