HDU 2102 拯救公主
需要注意的是,骑士到达时空传输机就立即回被传输到第二层,除非另一层的相对位置是传输机或墙。
1 #include <algorithm> 2 #include <map> 3 #include <vector> 4 #include <functional> 5 #include <string> 6 #include <cstring> 7 #include <queue> 8 #include <set> 9 #include <cmath> 10 #include <cstdio> 11 using namespace std; 12 #define IOS ios_base::sync_with_stdio(false) 13 #define TIE std::cin.tie(0) 14 #define MIN2(a,b) (a<b?a:b) 15 #define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c)) 16 #define MAX2(a,b) (a>b?a:b) 17 #define MAX3(a,b,c) (a>b?(a>c?a:c):(b>c?b:c)) 18 typedef long long LL; 19 typedef unsigned long long ULL; 20 const int INF = 0x3f3f3f3f; 21 const double PI = 4.0*atan(1.0); 22 23 int c, n, m, t, gx, gy, gz; 24 typedef struct Piont{ 25 int z, x, y, t; 26 Piont(int z = 0, int x = 0, int y = 0, int t = 0) :z(z), x(x), y(y), t(t){}; 27 }P; 28 29 char board[2][15][15]; 30 bool vis[2][15][15]; 31 int dx[4] = { 0, 0, 1, -1 }, dy[4] = { 1, -1, 0, 0 }; 32 bool solve() 33 { 34 memset(vis, 0, sizeof(vis)); 35 queue<P> que; 36 que.push(P()); 37 vis[0][0][0] = true; 38 while (que.size()){ 39 P p = que.front(); que.pop(); 40 if (p.t > t) return false; 41 if (p.z == gz && p.x == gx && p.y == gy) return true; 42 int nx, ny, nz; 43 44 for (int i = 0; i < 4; i++){ 45 nz = p.z, nx = p.x + dx[i], ny = p.y + dy[i]; 46 if (0 <= nx && nx < n && 0 <= ny && ny < m 47 &&board[nz][nx][ny] != '*'&&!vis[nz][nx][ny]){ 48 if (board[nz][nx][ny] == '#') nz ^= 1; 49 if (board[nz][nx][ny] != '#'&& board[nz][nx][ny]!='*'){ 50 que.push(P(nz, nx, ny, p.t + 1)); 51 vis[nz][nx][ny] = true; 52 } 53 } 54 } 55 } 56 return false; 57 } 58 int main() 59 { 60 scanf("%d", &c); 61 while (c--){ 62 scanf("%d%d%d", &n, &m, &t); 63 for (int i = 0; i < 2; i++){ 64 for (int j = 0; j < n; j++){ 65 getchar(); 66 for (int k = 0; k < m; k++){ 67 board[i][j][k] = getchar(); 68 if (board[i][j][k] == 'P') 69 gz = i, gx = j, gy = k; 70 } 71 } 72 getchar(); 73 } 74 printf("%s\n", solve() ? "YES" : "NO"); 75 } 76 }