#include <iostream> #include <cstdio> #include <queue> using namespace std; int dir[4][2]={0,1,0,-1,1,0,-1,0}; struct node { int x; int y; int z; int time; }; int sx,sy,sz,ex,ey,ez,N,M,T,mark[3][100][100]; char s[3][100][100]; void BFS() { queue<node>Q; node p,q; p.x=sx; p.y=sy; p.z=sz; p.time=0; mark[sz][sx][sy]=1; Q.push(p); while(!Q.empty()) { q=Q.front(); Q.pop(); if (q.time>T) break; if (q.time<=T&&q.x==ex&&q.y==ey&&q.z==ez) { printf ("YES\n"); return; } int i; for (i=0;i<4;++i) { p=q; p.x+=dir[i][0]; p.y+=dir[i][1]; if (p.x>=1&&p.x<=N&&p.y>=1&&p.y<=M&&p.z>=1&&p.z<=2&&s[p.z][p.x][p.y]!='*'&&!mark[p.z][p.x][p.y]) { mark[p.z][p.x][p.y]=1; //位置到达终点时也要加上时间,可根据题目点的测试案例可知 if (s[p.z][p.x][p.y]=='.'||s[p.z][p.x][p.y]=='P') p.time=q.time+1; if (s[p.z][p.x][p.y]=='#') { if (p.z==1) p.z=2; else p.z=1; p.time=q.time+1; mark[p.z][p.x][p.y]=1; //如果进入了时空穿梭机,那么两个位置都要标记 } Q.push(p); //采用该行跟踪进入队列的数,以便可以调试,通过这样输出可得到 //原来已入队的元素可能会再次入队,但没有影响结果 //也可通过对时空穿梭机的对边进行判断,已阻止其入队 cout<<p.x<<" "<<p.y<<" "<<p.z<<" "<<p.time<<endl; } } } printf ("NO\n"); } int main() { int k,i,j,l; cin>>k; while(k--) { cin>>N>>M>>T; for (l=1;l<=2;++l) for (i=1;i<=N;++i) for (j=1;j<=M;++j) { cin>>s[l][i][j]; if (s[l][i][j]=='S') sx=i,sy=j,sz=l; if (s[l][i][j]=='P') ex=i,ey=j,ez=l; mark[l][i][j]=0; } l=1; for (i=1;i<=N;++i) for (j=1;j<=M;++j) { if (s[l][i][j]==s[l+1][i][j]&&s[l][i][j]=='#') s[l][i][j]=s[l+1][i][j]='*'; if (s[l][i][j]=='*'&&s[l+1][i][j]=='#') s[l][i][j]=s[l+1][i][j]='*'; if (s[l][i][j]=='#'&&s[l+1][i][j]=='*') s[l][i][j]=s[l+1][i][j]='*'; } BFS(); } return 0; }