先预处理把所有通过传送门之后不可能的情况都把传送门换成墙
还有就是遇到传送门会到第二层.用异或方便一些。因为0^1 = 1. 1 ^ 1 = 0; 遇到传送门就异或一次,表示到了另外一层迷宫
最后起点到P点的最短距离小于T, 返回真
/***********************************************
* Author: fisty
* Created Time: 2015/2/13 15:41:32
* File Name : two_I.cpp
*********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define MAX_N 20
int n, m, t;
char G[MAX_N][MAX_N][2];
int vis[MAX_N][MAX_N][2];
int d[MAX_N][MAX_N][2];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
struct Point{
int first;
int second;
int z;
Point(int _x, int _y, int _z):first(_x), second(_y), z(_z){}
};
bool bfs(){
Memset(d, 0);
d[0][0][0] = 0;
vis[0][0][0] = 1;
queue<Point> que;
que.push(Point(0, 0, 0));
while(que.size()){
Point p = que.front(); que.pop();
if(G[p.first][p.second][p.z] == 'P') {
//如果可以到达
if(d[p.first][p.second][p.z] <= t)
return true;
return false;
}
for(int i = 0;i < 4; i++){
int x = p.first + dx[i];
int y = p.second + dy[i];
int z = p.z;
if(G[x][y][z] == '#'){
//进入下一层
vis[x][y][z] = 1;
z = z^1;
}
if(!vis[x][y][z] && G[x][y][z] != '*' && x < n && x >= 0 && y < m && y >=0){
vis[x][y][z] = 1;
que.push(Point(x, y, z));
d[x][y][z] = d[p.first][p.second][p.z] + 1;
}
}
}
return false;
}
int main() {
//freopen("in.cpp", "r", stdin);
cin.tie(0);
ios::sync_with_stdio(false);
int Kcase;
cin >> Kcase;
while(Kcase--){
cin >> n >> m >> t;
Memset(G, 0);
FOR(k, 0, 2){
FOR(i, 0, n){
FOR(j, 0, m){
cin >> G[i][j][k];
}
}
}
//预处理,把传送门不可以的地方都换成墙
FOR(i, 0, n){
FOR(j, 0, m){
if(G[i][j][0] == '#' && G[i][j][1] == '#'){
G[i][j][1] = G[i][j][0] = '*';
}else if(G[i][j][0] == '#' && G[i][j][1] == '*'){
G[i][j][0] = G[i][j][1] = '*';
}else if(G[i][j][0] == '*' && G[i][j][1] == '#'){
G[i][j][0] = G[i][j][1] = '*';
}
}
}
Memset(vis, 0);
if(bfs())
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}