본문 바로가기

algorithm

백준 6593번 상범 빌딩

https://www.acmicpc.net/problem/6593

 

아이디어:

1. 3차원 BFS

2. board[z][x][y], int dz[6] = { 1, -1, 0, 0, 0, 0 }; int dx[6] = { 0, 0, 1, 0, -1, 0 }; int dy[6] = { 0, 0, 0, 1, 0, -1 };, int nz, nx, ny 순서에 주의

3. tuple<int, int, int> 사용

 

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

char board[31][31][31];
int vis[31][31][31];
int dz[6] = { 1, -1, 0, 0, 0, 0 };
int dx[6] = { 0, 0, 1, 0, -1, 0 };
int dy[6] = { 0, 0, 0, 1, 0, -1 };

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	queue<tuple<int, int ,int>> Q;
	
	while (true) {

		int L, R, C;
		cin >> L >> R >> C;
		if (L == 0) return 0;

		string tmp;
		bool isPossible = false;
		int ans = 0;

		for (int i = 0; i < 31; i++) {
			for (int j = 0; j < 31; j++) {
				fill(board[i][j], board[i][j] + 31, '0');
				fill(vis[i][j], vis[i][j] + 31, 0);
			}
		}

		for (int i = 0; i < L; i++) {
			for (int j = 0; j < R; j++) {
				cin >> tmp;
				int k = 0;
				for (auto x : tmp) {
					board[i][j][k] = x;
					if (x == 'S') {
						vis[i][j][k] = 0;
						Q.push({ i, j, k });
					}
					k++;
				}
			}
		}

		while (!Q.empty()) {
			tuple<int, int, int> cur = Q.front();
			Q.pop();

			for (int dir = 0; dir < 6; dir++) {
				int nz = get<0>(cur) + dz[dir];
				int nx = get<1>(cur) + dx[dir];
				int ny = get<2>(cur) + dy[dir];
				if (nz < 0 || nz >= L || nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
				if (board[nz][nx][ny] == 'E') {
					ans = vis[get<0>(cur)][get<1>(cur)][get<2>(cur)] + 1;
					isPossible = true;
					while (!Q.empty()) Q.pop();
				}
				if (board[nz][nx][ny] != '.' || vis[nz][nx][ny] != 0) continue;
				vis[nz][nx][ny] = vis[get<0>(cur)][get<1>(cur)][get<2>(cur)] + 1;
				Q.push({ nz, nx, ny });
			}
		}

		if (isPossible) cout << "Escaped in " << ans << " minute(s).\n";
		else cout << "Trapped!\n";
	}

	return 0;

}

'algorithm' 카테고리의 다른 글

백준 2468번 안전 영역  (0) 2025.05.07
백준 5014번 스타트링크  (0) 2025.04.27
백준 2583번 영역 구하기  (0) 2025.04.12
백준 5427번 불  (0) 2025.04.12
백준 7562번 나이트의 이동  (0) 2025.04.12