HDU 2653 Waiting ten thousand years for Love(bfs+优先队列)

本文介绍了一个关于策略和算法的问题,主人公要在有限时间内穿越迷宫击杀恶魔。文章详细解释了迷宫地图符号,行走与飞行的不同成本,并提供了一段C++代码实现。该算法使用三维数组记录状态,通过优先队列进行广度优先搜索,以找到最短路径。
摘要由CSDN通过智能技术生成

Waiting ten thousand years for Love

Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1166    Accepted Submission(s): 378


Problem Description
It was ten thousand years, after Demon Lemon caught Yifenfei’s love. In order to revenge and save his love, Yifenfei have been practising sword all day long and his Kongfu skills becomes so powerful that he can kill Demon Lemon immediately. Recently, Yifenfei have found Lemon’s castle, and now he is going to kill Lemon. At the same time, hearing about the terrible news, Demon Lemon is now preparing for escaping...

Now Yifenfei has got the map of the castle.
Here are all symbols of the map:
Only one ‘Y’ Indicates the start position of Yifenfei.
Only one ‘L’ Indicates the position of Demon Lemon.
‘.’ Indicate the road that Yifenfei can walk on it, or fly over it.
‘#’ Indicate the wall that Yifenfei can not walk or flay through it.
‘@’ Indicate the trap that Yifenfei can not walk on it, but can fly over it.

Yifenfei can walk or fly to one of up, down, left or right four directions each step, walk costs him 2 seconds per step, fly costs him 1 second per step and 1 magic power. His magic power will not increased, and if his magic power is zero, he can not fly any more.

Now Yifenfei asks you for helping him kill Demon Lemon smoothly. At the same time, Demon Lemon will Leave the castle Atfer T seconds. If Yifenfei can’t kill Demon Lemon this time, he have to wait another ten thousand years.
 

Input
Lots of test cases, please process to end of file. In each test case, firstly will have four integers N, M, T, P(1 <= N, M, P <= 80, 1 <= T <= 100000), indicates the size of map N * M, the Lemon’s leaving time(after T seconds, Lemon will disappeared) and Yifenfei’s magic power. Then an N * M two-dimensional array follows indicates the map.
 

Output
For each test case, first Print a line “Case C:”, C indicates the case number. Then, if Yifenfei can kill Demon Lemon successfully, Print “Yes, Yifenfei will kill Lemon at T sec.”, T indicates the minimum seconds he must cost. Otherwise, Print ”Poor Yifenfei, he has to wait another ten thousand years.”
 

Sample Input
 
 
2 3 2 2
Y@L
###
2 3 4 1
Y@L
###
2 3 4 0
Y.L
###
2 3 3 0
Y.L
###
 
Sample Output
 
 
Case 1:
Yes, Yifenfei will kill Lemon at 2 sec.
Case 2:
Poor Yifenfei, he has to wait another ten thousand years.
Case 3:
Yes, Yifenfei will kill Lemon at 4 sec.
Case 4:
Poor Yifenfei, he has to wait another ten thousand years.
Hint
HintCase 1: Yifenfei cost 1 second and 1 magic-power fly to ‘@’, but he can not step on it, he must cost another 1 second and 1 magic-power fly to ‘L’ and kill Lemon immediately. Case 2: When Yifenfei Fly to ‘@’, he has no power to fly, and is killed by trap.
         做完诡异的楼梯,做的这题,以为二者差不多的,不能在'@'上停留,还以为在'@'上不能随意飞呢,像诡异的楼梯一样必须一下飞过'@', 也是用二维数组记录当前最小值和优先队列呢,后来wa了,一搜博客才知道理解错了,结果是我想多了,看来是中楼梯的毒太深! 
        因为每步走完剩余的魔法值不一样,所以用三维数组标记走过的状态,加上优先队列。总共分为二种情况,1、只能步行的,例从'.'走到'.',或'Y'、'L'这样的要花费2s,不浪费法力值。2、从'@'走向别的地方,或从别的地方走向'@',要飞行,花费1s,浪费1法力值


#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,T,p;
int a,b;
int f[4][2]={0,1,1,0,-1,0,0,-1};
char mat[85][85];
int vis[85][85][85];
struct node{
	int x,y;
	int temp;//时间花费
	int magic;//当前还有的魔法值 
	friend bool operator<(node a,node b){
		return a.temp >b.temp ;//升序 
	}
};
void bfs()
{
	priority_queue<node>q;
	node s,t;
	s.x =a;
	s.y =b;
	s.magic=p;
	s.temp =0;
	vis[a][b][p]=1;
	q.push(s);
	while(!q.empty())
	{
		t=q.top();
		q.pop();
		if(mat[t.x][t.y]=='L'&&t.temp <=T)//成功 
		{
			printf("Yes, Yifenfei will kill Lemon at %d sec.\n",t.temp );
			return ;
		}
		for(int i=0;i<4;i++)
		{
			s.x=t.x +f[i][0];
			s.y=t.y +f[i][1];
			s.magic =t.magic ;
			if(s.x<0||s.y<0||s.x>=n||s.y>=m||mat[s.x][s.y]=='#')//判断边界 
				continue;
			if(mat[s.x][s.y]!='@'&&mat[t.x][t.y]!='@'&&!vis[s.x][s.y][s.magic]&&t.temp+2<=T)//第一种情况 
			{
				vis[s.x][s.y][s.magic]=1;//标记已走坐标和当前魔法值,代表这个状态 
				s.temp =t.temp+2;
				q.push(s);
			}
			if(t.magic>0&&!vis[s.x][s.y][t.magic-1]&&t.temp +1<=T)//第二种情况,t.temp=1<=T一种剪枝
			{
				s.magic--;//魔法值减1 
				vis[s.x][s.y][s.magic]=1;
				s.temp =t.temp +1;
					q.push(s);
			}	
		}		
	}
	printf("Poor Yifenfei, he has to wait another ten thousand years.\n");//不能成功 
	return;
}
int main()
{
	int c=0;
	while(~scanf("%d%d%d%d",&n,&m,&T,&p))
	{
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++)
			scanf("%s",mat[i]);
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(mat[i][j]=='Y')
				{
					a=i,b=j;
				}
			}
		}
		printf("Case %d:\n",++c);
		bfs();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值