HDU 1242 Rescue

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24500    Accepted Submission(s): 8643


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... ..#..#.# #...##.. .#...... ........
 

Sample Output
  
  
13

题意:某人被关在囚笼里等待朋友解救,问能否解救成功,最少需要多少时间~
具体:可同时有几个朋友,每走一格消耗一分钟的时间 ,地图上还存在着卫兵,卫兵可以解决掉,
但是要另外花费一分钟~分析:从“a”出发,此题可以用回溯法进行深搜,但那样做的话,效率还是不能让人满意,但是广搜的话,
由于入队后每次出队时,根据地图情况的不同,出队元素所记忆的时间并不是层次递增的,因此使用简单广搜的话,
同样需要全部搜索才能找到正确答案。有没有一种方法能让某一步因为遇到士兵而多花时间的结点在队列中向后
推迟一层出队呢?答案是肯定的,在这里我们可以用优先队列来实现,总体思想上是,根据时间进行优先性选择,
每次都要出队当前队列元素中记录时间最少的出队,而入队处理时,我们可以按顺序对四个方向上的各种情况按正
常处理入队就行了,出队顺序由优先队列根据预设优先性自动控制。这样,我们就可以从“a”进行基于优先队列
的范围搜索了,并且在第一次抵达有朋友的位置时得到正确结果~
题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙
路花费一秒,x花费两秒
问到达终点的最少时间


#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
char map[205][205];
int vis[205][205];
struct node
{
	int x,y,time;
	bool operator <(const  node &b)const
	{
		return b.time<time;
	}
};
priority_queue<node> p;
node q;
void add(int x, int y, int t)
{
	q.x = x + 1; q.y = y;
	if(map[q.x][q.y] != '#' && !vis[q.x][q.y])
	{
		if(map[q.x][q.y] == 'x')
			q.time = t + 2;
		else
			q.time = t + 1;
		p.push(q);
	}
	q.x = x - 1; q.y = y;
	if(map[q.x][q.y] != '#' && !vis[q.x][q.y])
	{
		if(map[q.x][q.y] == 'x')
			q.time = t + 2;
		else
			q.time = t + 1;
		p.push(q);
	}
	q.x = x; q.y = y + 1;
	if(map[q.x][q.y] != '#' && !vis[q.x][q.y])
	{
		if(map[q.x][q.y] == 'x')
			q.time = t + 2;
		else
			q.time = t + 1;
		p.push(q);
	}
	q.x = x; q.y = y - 1;
	if(map[q.x][q.y] != '#' && !vis[q.x][q.y])
	{
		if(map[q.x][q.y] == 'x')
			q.time = t + 2;
		else
			q.time = t + 1;
		p.push(q);
	}
}
void bfs()
{
	int i, x, y, t;
	while(!p.empty())
	{
		q = p.top();
		p.pop();
		x = q.x;
		y = q.y;
		t = q.time;
		if(map[x][y] == 'a')
		{
			printf("%d\n", q.time);
			while(!p.empty())
				p.pop();
			return;
		}
		if(map[x][y] != '#' && !vis[x][y])
		{
			vis[x][y] = 1;
			add(x, y, q.time);
		}
	}
	printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
	int n, m, i, j;
	while(scanf("%d%d", &n, &m) != EOF) 
	{
		memset(map, '#', sizeof(map));
		memset(vis, 0, sizeof(vis));
		for(i = 1; i <= n; i++)
		{
			getchar();
			for(j = 1; j <= m; j++)
			{
				scanf("%c", &map[i][j]);
				if(map[i][j] == 'r')
				{
					q.x = i;
					q.y = j;
					q.time = 0;
					p.push(q);
				}
			}
		}
		bfs();
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值