**
BFS
**
//BFS
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n, m, T;
typedef struct Node {
int x, y, z;
int t;
}Node;
int A[2][15][15];
int ans[2][15][15]; //存储状态
int vis[2][15][15]; //是否走过
int sx, sy, sz, ex, ey, ez;
int arr[4][2] = { {0,-1},{0,1},{1,0},{-1,0} }; //四个方向
queue<Node>q;
bool check(int x, int y, int z) {
if (x >= 0 && x < 2 && y >= 0 && y < n&&z >= 0 && z < m) {
//不能是墙,时空机后面不能是时空机,且没有走过
if (A[x][y][z] && !vis[x][y][z] && A[x][y][z] != 2)return 1;
else return 0;
}
return 0;
}
void update_ans(Node &s) {
//更新状态
if (ans[s.x][s.y][s.z] < 0 || s.t < ans[s.x][s.y][s.z])ans[s.x][s.y][s.z] = s.t;
}
void BFS() {
Node start;
start.x = sx; start.y = sy; start.z = sz;
start.t = 0;
vis[sx][sy][sz] = 1;
q.push(start);
while (!q.empty()) {
Node v = q.front(); q.pop();
int i;
update_ans(v);
if (ans[ex][ey][ez] >= 0)return;
for (i = 0; i < 4; i++) {
Node t = v;
t.y += arr[i][0]; t.z += arr[i][1];
t.t++;
if (A[t.x][t.y][t.z] == 2) { t.x = (t.x + 1) % 2; }
if (check(t.x, t.y, t.z)) {
vis[t.x][t.y][t.z] = 1;
q.push(t);
}
}
}
}
int main() {
int C;
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &C);
while (C--) {
scanf("%d%d%d", &n, &m, &T);
int i, j, k;
while (!q.empty())q.pop();
memset(A, 0, sizeof(A));
for (i = 0; i < 2; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < m; k++) {
char ch;
scanf("%c", &ch);
while (ch == '\n' || ch == ' ' || ch == '\0' || ch == '?')scanf("%c", &ch);
if (ch == 'S') {
A[i][j][k] = 1; sx = i;
sy = j; sz = k;
}
else if (ch == 'P') {
A[i][j][k] = 9; ex = i;
ey = j; ez = k;
}
else if (ch == '*')A[i][j][k] = 0;
else if (ch == '#')A[i][j][k] = 2;
else if (ch == '.')A[i][j][k] = 1;
}
}
}
memset(ans, -1, sizeof(ans));
memset(vis, 0, sizeof(vis));
BFS();
if (ans[ex][ey][ez] >= 0 && ans[ex][ey][ez] <= T)printf("YES\n");
else printf("NO\n");
}
return 0;
}
DFS
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n, m, T;
int A[2][15][15];
int vis[2][15][15];
int sx, sy, sz, ex, ey, ez;
int arr[4][2] = { {0,-1},{0,1},{1,0},{-1,0} };
bool check(int x, int y, int z) {
if (x >= 0 && x < 2 && y >= 0 && y < n&&z >= 0 && z < m) {
if (A[x][y][z] && !vis[x][y][z] && A[x][y][z] != 2)return 1;
else return 0;
}
return 0;
}
int DFS(int x, int y, int z,int cur) {
if (cur > T)return 0;
if (A[x][y][z] == 9 && cur <= T)return 1;
else {
int i;
for (i = 0; i < 4; i++) {
int xx, yy, zz;
yy = y + arr[i][0];
zz = z + arr[i][1];
xx = x;
if (A[xx][yy][zz] == 2)xx = (x + 1) % 2;
if (check(xx,yy,zz)) {
vis[xx][yy][zz] = 1;
int flg = DFS(xx, yy, zz, cur + 1);
if (flg)return flg;
vis[xx][yy][zz] = 0;
}
}
}
return 0;
}
int main() {
int C;
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &C);
while (C--) {
scanf("%d%d%d", &n, &m, &T);
int i, j, k;
memset(A, 0, sizeof(A));
for (i = 0; i < 2; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < m; k++) {
char ch;
scanf("%c", &ch);
while (ch == '\n' || ch == ' ' || ch == '\0' || ch == '?')scanf("%c", &ch);
if (ch == 'S') {
A[i][j][k] = 1; sx = i;
sy = j; sz = k;
}
else if (ch == 'P') {
A[i][j][k] = 9; ex = i;
ey = j; ez = k;
}
else if (ch == '*')A[i][j][k] = 0;
else if (ch == '#')A[i][j][k] = 2;
else if (ch == '.')A[i][j][k] = 1;
}
}
}
memset(ans, -1, sizeof(ans));
memset(vis, 0, sizeof(vis));
int flg = DFS(sx, sy, sz, 0);
if (flg)printf("YES\n");
else printf("NO\n");
}
return 0;
}