https://vjudge.net/contest/402248#problem/I
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在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
Sponsor
代码:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include<time.h>
using namespace std;
#define ll long long
const int mod=1e9+7;
const int maxn=2e2+10;
ll n,m,s,k,tt;
char a[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int x,y,z,ex,ey,ez,ans;
struct node{
int nx;
int ny;
int nz;
int t;
}p;
queue<node>q;
bool judge(int xx,int yy,int zz)
{
if(xx==ex&&yy==ey&&zz==ez)
return true;
if(xx>=0&&xx<n&&yy>=0&&yy<m&&zz>=0&&zz<k&&a[xx][yy][zz]=='.')
return true;
if(xx>=0&&xx<n&&yy>=0&&yy<m&&zz>=0&&zz<k&&a[xx][yy][zz]=='#')
return true;
return false;
}
void bfs()
{
while(!q.empty())
{
q.pop();
}
p={x,y,z,0};
q.push(p);
while(!q.empty())
{
node u;
u=q.front();
q.pop();
if(u.nx==ex&&u.ny==ey&&u.nz==ez)
{
ans=u.t;
break;
}
if(a[u.nx][u.ny][u.nz]=='#')
{
u.nx=1-u.nx;
if(vis[u.nx][u.ny][u.nz]==0&&a[u.nx][u.ny][u.nz]=='P')
{
ans=u.t;
break;
}
if(vis[u.nx][u.ny][u.nz]==0&&a[u.nx][u.ny][u.nz]=='.')
vis[u.nx][u.ny][u.nz]=1;
else
continue;
}
if(judge(u.nx,u.ny+1,u.nz)&&vis[u.nx][u.ny+1][u.nz]==0)
{
vis[u.nx][u.ny+1][u.nz]=1;
q.push({u.nx,u.ny+1,u.nz,u.t+1});
}
if(judge(u.nx,u.ny-1,u.nz)&&vis[u.nx][u.ny-1][u.nz]==0)
{
vis[u.nx][u.ny-1][u.nz]=1;
q.push({u.nx,u.ny-1,u.nz,u.t+1});
}
if(judge(u.nx,u.ny,u.nz+1)&&vis[u.nx][u.ny][u.nz+1]==0)
{
vis[u.nx][u.ny][u.nz+1]=1;
q.push({u.nx,u.ny,u.nz+1,u.t+1});
}
if(judge(u.nx,u.ny,u.nz-1)&&vis[u.nx][u.ny][u.nz-1]==0)
{
vis[u.nx][u.ny][u.nz-1]=1;
q.push({u.nx,u.ny,u.nz-1,u.t+1});
}
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
n=2;
scanf("%lld %lld %lld",&m,&k,&tt);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%s",a[i][j]);
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
for(int u=0;u<k;u++)
{
vis[i][j][u]=0;
if(a[i][j][u]=='S')
{
x=i;
y=j;
z=u;
}
else if(a[i][j][u]=='P')
{
ex=i;
ey=j;
ez=u;
}
}
}
}
ans=1e9+7;
bfs();
//cout<<ans<<endl;
if(ans<=tt)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}