티스토리 뷰
문제 출처: https://www.acmicpc.net/problem/6593
6593번: 상범 빌딩
당신은 상범 빌딩에 갇히고 말았다. 여기서 탈출하는 가장 빠른 길은 무엇일까? 상범 빌딩은 각 변의 길이가 1인 정육면체(단위 정육면체)로 이루어져있다. 각 정육면체는 금으로 이루어져 있어
www.acmicpc.net
3차원 BFS로 문제를 해결할 수 있다.
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int movepos[6][3] = { {0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0} };
int L, R, C;
char arr[31][31][31];
int visited[31][31][31];
bool canMove(int x, int y, int z) {
if (x < 0 || x >= C)return false;
if (y < 0 || y >= R)return false;
if (z < 0 || z >= L)return false;
if (arr[z][y][x] == '.' || arr[z][y][x] == 'E')return true;
return false;
}
typedef struct {
int z, y, x,d;
}P;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
while (true) {
fill(&visited[0][0][0], &visited[30][30][31], 0);
cin >> L >> R >> C;
if (L == 0 && R == 0 && C == 0)break;
queue <P> q;
for (int i = 0; i < L; i++) {
for (int j = 0; j < R; j++) {
for (int k = 0; k < C; k++) {
cin >> arr[i][j][k];
if (arr[i][j][k] == 'S') {
visited[i][j][k] = 1;
q.push(P{ i,j,k,0 });
}
}
}
}
int flag = 1;
while (!q.empty()) {
P curr = q.front(); q.pop();
if (arr[curr.z][curr.y][curr.x] == 'E') {
flag = 0;
cout << "Escaped in "<<curr.d<<" minute(s).\n";
}
for (int i = 0; i < 6; i++) {
int gox, goy,goz;
gox = curr.x + movepos[i][0];
goy = curr.y + movepos[i][1];
goz = curr.z + movepos[i][2];
if (canMove(gox, goy, goz)) {
if(visited[goz][goy][gox])continue;
visited[goz][goy][gox] = 1;
q.push(P{ goz,goy,gox,curr.d + 1 });
}
}
}
if (flag == 1)cout << "Trapped!\n";
}
}
'PS' 카테고리의 다른 글
| [백준] 6018번 Tea Time (0) | 2022.05.13 |
|---|---|
| [백준] 7511번 소셜 네트워킹 어플리케이션 C++ (0) | 2022.05.13 |
| [백준] 11779번 최소비용 구하기 2 C++ (1) | 2022.05.10 |
| [백준] 1738번 골목길 C++ (0) | 2022.05.10 |
| [백준] 11657번 타임머신 C++ (0) | 2022.05.10 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 벨만-포드
- 타임머신
- 11657
- tea time
- 1004
- 후위 표기식
- 어린왕자 C++
- 골목길C++
- 스택
- 1918
- 중위 표기식 후위 표기식으로 변환
- 6539
- 백준
- 7511
- 소셜네트워킹어플리케이션
- C++
- 골목길
- 6018
- 1738
- 상범빌딩
- 벨만포드
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함
