본문 바로가기
Algorithm/C++

[백준 6593] 상범 빌딩 (tuple 사용법)

by imagineer_jinny 2022. 9. 6.

6593번: 상범 빌딩 (acmicpc.net)

 

6593번: 상범 빌딩

당신은 상범 빌딩에 갇히고 말았다. 여기서 탈출하는 가장 빠른 길은 무엇일까? 상범 빌딩은 각 변의 길이가 1인 정육면체(단위 정육면체)로 이루어져있다. 각 정육면체는 금으로 이루어져 있어

www.acmicpc.net

 

배운것

- 3차원이라고 쫄 것 없음. L(층) 하나만 추가해주면 되고 다 똑같음

- tuple 사용하기

tuple<int, int, int> ti;
int a, b, c;
int main(){	
	ti = make_tuple(1, 2, 3);
	a = get<0>(ti);
	b = get<1>(ti);
	c = get<2>(ti);
	cout << a << " : " << b << " : "<< c << "\n";
	return 0;
}

// 1 : 2 : 3

 

 

정답 풀이

// Authored by : yongjunleeme
// Co-authored by : -
// http://boj.kr/5c5193d29eee450394adefa11f8c8f23
#include <bits/stdc++.h>
using namespace std;
const int MX = 31;
int dx[6] = {0, 0, 1, -1, 0, 0};
int dy[6] = {1, -1, 0, 0, 0, 0};
int dh[6] = {0, 0, 0, 0, 1, -1};
int l, r, c;

int main(void){
  ios::sync_with_stdio(0);
  cin.tie(0);
  while(1){
    cin >> l >> r >> c;
    if(l == 0 && r == 0 && c == 0) break;
    queue<tuple<int,int,int>> Q;
    char board[MX][MX][MX];
    int dist[MX][MX][MX];
    bool isEscape = false;

    for(int i = 0; i < l; i++)
      for(int j = 0; j < r; j++) fill(dist[i][j], dist[i][j] + c, 0);

    for(int i = 0; i < l; i++){
      for(int j = 0; j < r; j++){
        for(int k = 0; k < c; k++){
          cin >> board[i][j][k];
          if(board[i][j][k] == 'S'){
            Q.push({i,j,k});
            dist[i][j][k] = 0;
          }
          else if(board[i][j][k] == '.') dist[i][j][k] = -1;
        }
      }
    }

    while(!Q.empty()){
      if(isEscape) break;
      auto cur = Q.front(); Q.pop();
      int curH, curX, curY;
      tie(curH, curX, curY) = cur;
      for(int dir = 0; dir < 6; dir++){
        int nh = curH + dh[dir];
        int nx = curX + dx[dir];
        int ny = curY + dy[dir];
        if(nx < 0 || nx >= r || ny < 0 || ny >= c || nh < 0 || nh >= l) continue;
        if(board[nh][nx][ny] == '#' || dist[nh][nx][ny] > 0) continue;

        dist[nh][nx][ny] = dist[curH][curX][curY] + 1;
        if(board[nh][nx][ny] == 'E'){
          cout << "Escaped in " << dist[nh][nx][ny] << " minute(s).\n";
          isEscape = true;
          break;
        }
        Q.push({nh,nx,ny});
      }
    }
    while(!Q.empty()) Q.pop();
    if(!isEscape) cout << "Trapped!" << "\n";
  }
}

'Algorithm > C++' 카테고리의 다른 글

[백준 1074] Z  (0) 2022.09.07
[백준 1629] 곱셈  (0) 2022.09.06
[백준 2468] 안전 영역  (0) 2022.09.06
[백준 5014] 스타트링크  (0) 2022.09.04
[백준 2667] 단지번호붙이기  (0) 2022.09.04

댓글