문제: 6593번: 상범 빌딩
//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <string>
#include <queue>
#include <set>
#include <iomanip>
#include <string.h>
#include <sstream>
#include <tuple>
#include <map>
#include <stack>
#include <queue>
#include <tuple>
using namespace std;
vector<vector<vector<char>>> building;
vector<vector<vector<int>>> visits;
queue<tuple<int, int, int>> nexts;
int l, r, c; // 층 수, 행, 열
int time_ = 0;
// 동, 서, 남, 북, 상, 하 (총 6방향)
int dx[6] = { 1, -1, 0, 0, 0, 0 };
int dy[6] = { 0, 0, 1, -1, 0, 0 };
int dz[6] = { 0, 0, 0, 0, 1, -1 };
int BFS() {
time_ = 0;
while (!nexts.empty()) {
int qs = nexts.size();
while (qs--) {
tuple<int, int, int> cur = nexts.front(); nexts.pop();
int z = get<0>(cur);
int x = get<1>(cur);
int y = get<2>(cur);
if (building[z][x][y] == 'E') {
return 1;
}
for (int i = 0; i < 6; i++) {
int nz = z + dz[i];
int nx = x + dx[i];
int ny = y + dy[i];
if (nz < 0 || nz > l - 1 || nx < 0 || nx > r - 1 || ny < 0 || ny > c - 1) continue;
if (building[nz][nx][ny] == '#' || visits[nz][nx][ny] == 1) continue;
nexts.push({ nz, nx, ny });
visits[nz][nx][ny] = 1;
}
}
time_++;
}
return 0; // 여기까지 온거면 탈출 못해..
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
while (true) {
cin >> l >> r >> c;
if (l == 0 && r == 0 && c == 0) break;
building = vector<vector<vector<char>>>(l, vector<vector<char>>(r, vector<char>(c)));
visits = vector<vector<vector<int>>>(l, vector<vector<int>>(r, vector<int>(c)));
cin.ignore();
int startZ, startX, startY;
nexts = queue<tuple<int, int, int>>();
for (int i = 0; i < l; i++) {
if (i > 0) {
string tmp;
getline(cin, tmp); // 공백 없애
}
for (int j = 0; j < r; j++) {
string line;
getline(cin, line);
for (int k = 0; k < c; k++) {
building[i][j][k] = line[k];
if (line[k] == 'S') {
startZ = i;
startX = j;
startY = k;
nexts.push({ startZ, startX, startY });
visits[startZ][startX][startY] = 1; // 방문 처리
}
}
}
}
if (BFS() == 1) {
cout << "Escaped in " << time_ << " minute(s).\n";
}
else {
cout << "Trapped!\n";
}
}
return 0;
}
그냥 3차원 BFS 문제.