可憐的公主在一次次被魔王擄走一次次被騎士們救回來之後,而今,不幸的她再一次面臨生命的考驗。魔王已經發出消息說将在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
#include<iostream>
#include<queue>
using namespace std;
int t, n, m;
int T;
char mp[20][20][20];
bool vis[20][20][20];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
struct node {
int x, y, z, t;
};
queue<node> q;
bool check(int z, int x, int y)
{
if (x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < 2 && vis[z][x][y] == 0 && mp[z][x][y] != '*') return 1;
else return 0;
}
void bfs()
{
while (!q.empty()) {
node now = q.front();
q.pop();
//cout << now.z << endl;
if (mp[now.z][now.x][now.y] == 'P') {
if (now.t > t) {
//cout << now.t << endl;
cout << "NO" << endl;
return;
}
else {
cout << "YES" << endl;
return;
}
}
else if (mp[now.z][now.x][now.y] == '#') {
//cout << now.z << " " << now.x << " " << now.y << " " << now.t << endl;
int z = now.z ^ 1;
node next;
next.x = now.x;
next.y = now.y;
next.z = z;
next.t = now.t;
if (check(z, next.x, next.y)==1) {
vis[next.z][next.x][next.y] = 1;
q.push(next);
}
}
else {
node next;
for (int i = 0; i <= 3; i++) {
next.x = dir[i][0] + now.x;
next.y = dir[i][1] + now.y;
next.z = now.z;
next.t = now.t + 1;
if (check(next.z, next.x, next.y) == 1) {
vis[next.z][next.x][next.y] = 1;
q.push(next);
}
}
}
}
cout << "NO" << endl;
//cout << 1;
}
int main()
{
cin >> T;
while (T--) {
cin >> n >> m >> t;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) cin >> mp[i][j];
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
if (mp[i][j][k] == 'S') {
node b;
b.z = i, b.x = j, b.y = k, b.t = 0;
vis[i][j][k] = 1;
q.push(b);
bfs();
}
}
}
}
q = queue<node>();
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) vis[i][j][k] = 0;
}
}
}
return 0;
}