hdu 2102 A计划(BFS)(H)

Problem Description
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。


Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。

Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。

Sample Input
  
  
1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..

Sample Output
  
  
YES

题意:从s点出发在t时间内(包括t)到达P点  迷宫为二层

‘*‘表示石头 不可走

 ‘#‘表示经过了就必须要穿越到另外一层  不花费时间

‘.‘可以走的路,但要花时间1分钟

s初始位置(0,0,0)固定

P为目的地的位置

思路:简单的BFS 就是注意几个容易错的点即可 

1.#到了就必须到另一层

2.时间<=t

3.如果不能达到P点  那也算是NO

#include<stdio.h>
#include<string.h>
#include<cstring>
#include<math.h>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
#define maxn 20
#define P(a,b,c) make_pair(a,make_pair(b,c))
typedef pair<int ,pair<int ,int > >  pai;
int n,m,t;
char maps[2][maxn][maxn];
bool vis[2][maxn][maxn];
int time[2][maxn][maxn];
int x,y,z;
int go1[4][3]={0,1,0,0,-1,0,1,0,0,-1,0,0};
int go2[2][3]={0,0,1,0,0,-1};
bool bfs()
{
	memset(vis,false,sizeof vis);
	memset(time,0,sizeof time);
	queue<pai>q;
	q.push(P(0,0,0));
	vis[0][0][0]=true;
	while(!q.empty())
	{
		pai p;
		p=q.front();
		q.pop();
		if(maps[p.second.second][p.first][p.second.first]=='#')
		{
			for(int i=0;i<2;i++)
			{
				int fx=p.first+go2[i][0];
				int fy=p.second.first+go2[i][1];
				int fz=p.second.second+go2[i][2];
				if(fx>=0&&fx<n&&fy>=0&&fy<m&&fz>=0&&fz<=1)
				{
					if(!vis[fz][fx][fy]&&maps[fz][fx][fy]=='.')
					{
						vis[fz][fx][fy]=true;
						time[fz][fx][fy]=time[p.second.second][p.first][p.second.first];
						q.push(P(fx,fy,fz));
					}
					else if(maps[fz][fx][fy]=='P')
					{
						time[fz][fx][fy]=time[p.second.second][p.first][p.second.first];
						if(time[fz][fx][fy]<=t)
						{
							printf("YES\n");
						}
						else
							printf("NO\n");
						return true;
					}
				}
			}
		}
		else
		{
			for(int i=0;i<4;i++)
			{
				int fx=p.first+go1[i][0];
				int fy=p.second.first+go1[i][1];
				int fz=p.second.second+go1[i][2];
				if(fx>=0&&fx<n&&fy>=0&&fy<m&&fz>=0&&fz<=1)
				{
					if(!vis[fz][fx][fy]&&(maps[fz][fx][fy]=='.'||maps[fz][fx][fy]=='#'))
					{
						vis[fz][fx][fy]=true;
						time[fz][fx][fy]=time[p.second.second][p.first][p.second.first]+1;
						q.push(P(fx,fy,fz));
					}
					else if(maps[fz][fx][fy]=='P')
					{
						time[fz][fx][fy]=time[p.second.second][p.first][p.second.first]+1;
						if(time[fz][fx][fy]<=t)
						{
							printf("YES\n");
						}
						else
							printf("NO\n");
						return true;
					}
				}
			}
		}

	}
	return false;
}
int main()
{

	int cas;
	scanf("%d",&cas);
	while(cas--)
	{
		scanf("%d%d%d",&n,&m,&t);
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<n;j++)
			{
				scanf("%s",maps[i][j]);
			}
		}
		if(!bfs())
		printf("NO\n");
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值