题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102
有两个要注意的地方
1,走到传送门是强制传送的。
2,如果传送到另一层又是一个传送门,会死循环,直接排除掉这个点。
#include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int Maxn = 4e5+10;
struct Node {
int i, j, k, step;
};
int N, M, T, dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
char G[5][15][15];
bool vis[5][15][15];
void bfs() {
memset(vis, false, sizeof(vis));
queue<Node> qu;
Node tmp;
bool ok = false;
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < N; ++j) {
for(int k = 0; k < M; ++k) {
if(G[i][j][k] == 'S') {
tmp.i = i; tmp.j = j; tmp.k = k;
tmp.step = 0;
ok = true; break;
}
}
if(ok) break;
}
if(ok) break;
}
vis[tmp.i][tmp.j][tmp.k] = true;
qu.push(tmp);
while (!qu.empty()) {
tmp = qu.front(); qu.pop();
if(G[tmp.i][tmp.j][tmp.k] == '#') tmp.i = (tmp.i+1)%2;
if(G[tmp.i][tmp.j][tmp.k] == '*' || G[tmp.i][tmp.j][tmp.k] == '#') continue;
if(tmp.step > T) {
printf("NO\n"); return;
}
if(G[tmp.i][tmp.j][tmp.k] == 'P') {
printf("YES\n"); return;
}
for(int i = 0; i < 4; ++i) {
Node now = tmp;
now.j += dir[i][0]; now.k += dir[i][1];
if(now.j < 0 || now.j > N-1 || now.k < 0 || now.k > M-1
|| vis[now.i][now.j][now.k] || G[now.i][now.j][now.k] == '*') continue;
now.step++;
vis[now.i][now.j][now.k] = true;
qu.push(now);
}
}
printf("NO\n");
}
int main(void)
{
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d%d", &N, &M, &T);
for(int i = 0; i < N; ++i) scanf("%s", G[0][i]);
for(int i = 0; i < N; ++i) scanf("%s", G[1][i]);
bfs();
}
return 0;
}