[백준] 6593번 상범 빌딩

2025. 6. 3. 14:22·백준 문제/BFS

문제: 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 문제.

'백준 문제/BFS' 카테고리의 다른 글
  • [백준] 2644번 촌수계산
  • [백준] 16948번 데스 나이트
  • [백준] 3055번 탈출
  • [백준] 29634번 Hotel
dubu0721
dubu0721
dubu0721 님의 블로그 입니다.
  • dubu0721
    dubu0721 님의 블로그
    dubu0721
  • 전체
    오늘
    어제
    • 분류 전체보기 (352)
      • 프로그래밍언어론 정리 (5)
      • 컴퓨터네트워크 정리 (5)
      • 알고리즘&자료구조 공부 (64)
        • it 취업을 위한 알고리즘 문제풀이 입문 강의 (60)
        • 학교 알고리즘 수업 (3)
        • 실전프로젝트I (0)
      • 백준 문제 (204)
        • 이분탐색 (7)
        • 투포인트 (10)
        • 그래프 (11)
        • 그리디 (24)
        • DP (25)
        • BFS (21)
        • MST (7)
        • KMP (4)
        • Dijkstra (3)
        • Disjoints Set (4)
        • Bellman-Ford (2)
        • 시뮬레이션 (3)
        • 백트래킹 (15)
        • 위상정렬 (5)
        • 자료구조 (25)
        • 기하학 (1)
        • 정렬 (11)
        • 구현 (8)
        • 재귀 (8)
        • 수학 (8)
        • 트리 (1)
      • 유니티 공부 (11)
        • 레트로의 유니티 게임 프로그래밍 에센스 (11)
        • 유니티 스터디 자료 (0)
        • C# 공부 (0)
      • 유니티 프로젝트 (48)
        • 케이크게임 (13)
        • 점토게임 (35)
      • 언리얼 공부 (10)
        • 이득우의 언리얼 프로그래밍 (10)
      • 진로 (1)
      • 논문 읽기 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    이벤트 트리거
    티스토리챌린지
    dp
    백트래킹
    그래프
    우선순위큐
    유니티 공부 정리
    큐
    유니티 프로젝트
    자료구조
    C#
    재귀
    맵
    백준
    바킹독
    골드메탈
    그리디
    유니티
    투포인터
    시뮬레이션
    오블완
    BFS
    이득우
    해시
    스택
    이분탐색
    수학
    정렬
    언리얼
    레트로의 유니티 프로그래밍
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dubu0721
[백준] 6593번 상범 빌딩

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.