-
3차원 토마토가 떠오르는 문제였다
기본적인 BFS구조를쓰면되고, 3차원이다보니 pair를 사용할수없는점을 유의해야한다.
3개일때 묶는것도 따로있기는 한데, 어떤건지 모르겠어서 그냥 구조체에서 생성자써서 BFS를 진행했다.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778#include<iostream>#include<queue>#include<cstring>using namespace std;struct pt {int l = 0; int r = 0; int c = 0;pt(int l, int r, int c) {this->l = l;this->r = r;this->c = c;}};int L, R, C;char map[30][30][30];int d[30][30][30];int dx[] = { 0,0,1,-1,0,0 };int dy[] = { 1,-1,0,0,0,0 };int dz[] = { 0,0,0,0,1,-1 };pt start = pt(0, 0, 0);pt ed = pt(0, 0, 0);int bfs() {queue<pt> q;q.push(start);d[start.l][start.r][start.c] = 0;while (!q.empty()) {int l = q.front().l; int r = q.front().r; int c = q.front().c;q.pop();for (int i = 0; i < 6; i++) {int nl = l + dz[i]; int nr = r + dx[i]; int nc = c + dy[i];if (nl >= 0 && nl < L && nr >= 0 && nr < R && nc >= 0 && nc < C) {if (map[nl][nr][nc] == '.' || map[nl][nr][nc] == 'E') {if (d[nl][nr][nc] == 0) {d[nl][nr][nc] = d[l][r][c] + 1;q.push(pt(nl, nr, nc));if (map[nl][nr][nc] == 'E') {return true;}}}}}}return false;}int main() {ios_base::sync_with_stdio(0);cin.tie(0);while (true) {cin >> L >> R >> C;if (L == 0 && R == 0 && C == 0) break;for (int i = 0; i < L; i++) {for (int j = 0; j < R; j++) {for (int k = 0; k < C; k++) {cin >> map[i][j][k];if (map[i][j][k] == 'S'){start.l = i; start.r = j; start.c = k;}if (map[i][j][k] == 'E'){ed.l = i; ed.r = j; ed.c = k;}}}}if (bfs()) {cout <<"Escaped in "<< d[ed.l][ed.r][ed.c] <<" minute(s)."<< '\n';}else {cout << "Trapped!" << '\n';}memset(d, 0, sizeof(d));}return 0;}http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter