1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
int solve() { int L,R,C; std::cin >> L >> R >> C; if(L == 0 && R == 0 && C == 0) { return 0; } std::vector map(L,std::vector<std::string> (R)); std::vector f(L, std::vector (R, std::vector<int> (C, 1e9))); int sx,sy,sz,ex,ey,ez; for(int i = 0; i < L; i++) { for(int j = 0; j < R; j++) { std::cin >> map[i][j]; for(int k = 0; k < C; k++) { if(map[i][j][k] == 'S') { sx = i; sy = j; sz = k; } if(map[i][j][k] == 'E') { ex = i; ey = j; ez = k; } } } }
auto bfs = [&]() -> void { f[sx][sy][sz] = 0; std::array<std::array<int,3>,6> next = {{{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}}}; std::queue<int> X,Y,Z; X.push(sx);Y.push(sy);Z.push(sz); while(!X.empty()) { int x = X.front(), y = Y.front(), z = Z.front(); X.pop();Y.pop();Z.pop(); for(int i = 0; i < 6; i++) { int tx = x + next[i][0], ty = y + next[i][1], tz = z + next[i][2]; if(tx < 0 || tx >= L || ty < 0 || ty >= R || tz < 0 || tz >= C) continue; if(map[tx][ty][tz] == '#') continue; if(f[tx][ty][tz] != 1e9) continue; f[tx][ty][tz] = f[x][y][z] + 1; X.push(tx);Y.push(ty);Z.push(tz); } } }; bfs(); if(f[ex][ey][ez] == 1e9) { std::cout << "Trapped!\n"; } else { std::cout << "Escaped in " << f[ex][ey][ez] << " minute(s).\n"; } return 1; }
|