#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <cmath> using namespace std; struct node { int x; int y; int z; int step; }; char Map[12][12][2]; bool vis[12][12][2]; int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; int n, m, t; int start[3]; bool check(int x, int y, int z) { if(x < 0 || y < 0 || x >= n || y >= m || Map[x][y][z] == '*') return 1; else return 0; } void bfs() { memset(vis, false, sizeof(vis)); queue<node> q; node a; a.x = start[0]; a.y = start[1]; a.z = start[2]; a.step = 0; vis[a.x][a.y][a.z] = true; q.push(a); while(!q.empty()) { node b; b = q.front(); q.pop(); for(int i = 0; i < 4; ++ i) { node c = b; c.x += dx[i]; c.y += dy[i]; c.step ++; if(c.step > t) break; if(check(c.x, c.y, c.z) || vis[c.x][c.y][c.z]) continue; vis[c.x][c.y][c.z] = true; if(Map[c.x][c.y][c.z] == '#') { c.z = !c.z; if(check(c.x, c.y, c.z) || vis[c.x][c.y][c.z] || Map[c.x][c.y][c.z] == '#') continue; vis[c.x][c.y][c.z] = true; } if(Map[c.x][c.y][c.z] == 'P') { cout << "YES" << endl; return ; } q.push(c); } } cout << "NO" << endl; } int main() { int T; cin >> T; while(T --) { cin >> n >> m >> t; for(int k = 0; k < 2; ++ k) { for(int i = 0; i < n; ++ i) { for(int j = 0; j < m; ++ j) { cin >> Map[i][j][k]; if(Map[i][j][k] == 'S') { start[0] = i; start[1] = j; start[2] = k; } } } } bfs(); } return 0; }
参考博客:https://blog.csdn.net/flytowns/article/details/86552772