简单三维BFS,需要注意的就是如果传送门进去的下一层还是传送门,则不可以进入。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 2005
#define inf 1<<28
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define FOR(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
using namespace std;
int n,m,t;
char Map[2][20][20];
struct kdq
{
int x,y,z,step;
} q[1000000];
int movex[4]= {0,0,1,-1};
int movey[4]= {1,-1,0,0};
int inmap(kdq a)
{
if(a.x>=0&&a.x<n&&a.y>=0&&a.y<m&&a.z>=0&&a.z<=1&&Map[a.z][a.x][a.y]!='*')
return 1;
return 0;
}
int visit[2][20][20];
int bfs()
{
kdq s;
s.x=0,s.y=0,s.z=0,s.step=0;
int num=0,cnt=0;
q[num++]=s;
visit[0][0][0]=1;
while(num>cnt)
{
kdq temp=q[cnt++];
if(Map[temp.z][temp.x][temp.y]=='P')
{
if(temp.step<=t)
return 1;
return 0;
}
for(int i=0; i<4; i++)
{
kdq tt;
tt=temp;
tt.x+=movex[i];
tt.y+=movey[i];
tt.step++;
if(inmap(tt))
{
if(Map[tt.z][tt.x][tt.y]=='#')//传送门这里稍微注意一下
{
tt.z^=1;
if(Map[tt.z][tt.x][tt.y]=='*'||Map[tt.z][tt.x][tt.y]=='#')
continue;
}
if(!visit[tt.z][tt.x][tt.y])
{
visit[tt.z][tt.x][tt.y]=1;
q[num++]=tt;
}
}
}
}
return 0;
}
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%d%d",&n,&m,&t);
for(int i=0; i<2; i++)
{
for(int j=0; j<n; j++)
{
for(int k=0; k<m; k++)
{
visit[i][j][k]=0;
cin>>Map[i][j][k];
}
}
}
if(bfs())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}