728x90
https://www.acmicpc.net/problem/6593
6593번: 상범 빌딩
당신은 상범 빌딩에 갇히고 말았다. 여기서 탈출하는 가장 빠른 길은 무엇일까? 상범 빌딩은 각 변의 길이가 1인 정육면체(단위 정육면체)로 이루어져있다. 각 정육면체는 금으로 이루어져 있어
www.acmicpc.net
기본적인 bfs + 이동 카운트 문제이다. 아마도 q에서 직접 지나온 칸 수를 갖고 있는 방법과 다른 배열을 선언해서 갖고 있는 방식이 될 것 같은데 나는 후자를 선택하였다.
3차원 bfs는 처음이었는데 2차원 bfs와 다를게 없었다.
#include <iostream>
#include <queue>
using namespace std;
int L, R, C;
char building[31][31][31];
int cnt[31][31][31];
int dr[6] = { 1,-1,0,0,0,0 };
int dc[6] = { 0,0,1,-1,0,0 };
int dl[6] = { 0,0,0,0,1,-1 };
bool check(int l, int r, int c) {
return l > -1 && l < L && r > -1 && r < R && c > -1 && c < C;
}
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
while (true) {
queue<pair<int, pair<int, int>>> q;
cin >> L >> R >> C;
if (L == 0 && R == 0 && C == 0)
break;
for (int k = 0; k < L; k++) {
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cnt[k][i][j] = 0;
cin >> building[k][i][j];
if (building[k][i][j] == 'S') {
q.push({ k, {i, j} });
}
}
}
}
/*
for (int k = 0; k < L; k++) {
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cout << building[k][i][j] << " ";
}
cout << endl;
}
cout << endl;
}*/
bool ch = false;
while (!q.empty()) {
int nowL = q.front().first;
int nowR = q.front().second.first;
int nowC = q.front().second.second;
q.pop();
if (building[nowL][nowR][nowC] == 'E') {
cout << "Escaped in " << cnt[nowL][nowR][nowC] << " minute(s)." << '\n';
ch = true;
break;
}
for (int i = 0; i < 6; i++) {
int nextL = nowL + dl[i];
int nextR = nowR + dr[i];
int nextC = nowC + dc[i];
if (check(nextL, nextR, nextC) && building[nextL][nextR][nextC] != '#') {
if (cnt[nextL][nextR][nextC] == 0) {
q.push({ nextL, {nextR, nextC} });
cnt[nextL][nextR][nextC] = cnt[nowL][nowR][nowC] + 1;
}
}
}
}
if(ch == false)
cout << "Trapped!" << '\n';
}
}
'백준 문제 풀이' 카테고리의 다른 글
백준 1655번 가운데를 말해요 (C++) (1) | 2023.09.11 |
---|---|
백준 9084번 동전(C++) (0) | 2023.09.09 |
백준 1766번 문제집 (C++) (1) | 2023.08.22 |
백준 2252번 줄세우기 (C++) (0) | 2023.08.22 |
백준 14391번 종이 조각 (C++) (0) | 2023.08.12 |