A计划

                           A计划

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13232    Accepted Submission(s): 3262


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
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
char map[2][120][120];
int vis[2][120][120],maxx,m,n;
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
struct node 
{
	int x,y,z;
	int step;
	friend bool operator < (node n1,node n2)
	{
		return n1.step>n2.step;
	}
}p,temp;
int judge(node s)
{
	if(s.x<0||s.y<0||s.x>=m||s.y>=n)
	return 1;
	if(map[s.z][s.x][s.y]=='*'||map[s.z][s.x][s.y]=='#')
	return 1;
	if(vis[s.z][s.x][s.y]||s.step>maxx)
	return 1;
	return 0; 
}
void bfs()
{
	priority_queue<node>q;
	p.x=0;p.y=0;p.z=0;
	p.step=0;
	vis[0][0][0]=1;
	q.push(p);
	while(!q.empty())
	{
		p=q.top();
		q.pop();
		for(int i=0;i<4;i++)
		{
			temp.x=p.x+dx[i];
			temp.y=p.y+dy[i];
			temp.step=p.step+1;
			if(map[p.z][temp.x][temp.y]=='#')
			{
				if(p.z)
				temp.z=0;
				else temp.z=1;
			}
			else temp.z=p.z;
			if(judge(temp)) 
			{
				continue;
			}
			if(map[temp.z][temp.x][temp.y]=='P')
			{
				printf("YES
");return ;
			}
			vis[temp.z][temp.x][temp.y]=1;
			q.push(temp);
		}
	}
	printf("NO
");
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(vis,0,sizeof(vis));
		memset(map,'',sizeof(map));
		scanf("%d%d%d",&m,&n,&maxx);
		int i,j,k;
		for(i=0;i<2;i++)
		{
		for(j=0;j<m;j++)
		scanf("%s",map[i][j]);
		}
		/*for(i=0;i<2;i++)
		for(j=0;j<m;j++)
		printf("%s
",map[i][j]);*/
		bfs();
	}
	return 0;
}


原文地址:https://www.cnblogs.com/playboy307/p/5273852.html