fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
백준 6599 (C++) The Tower of Babylon
최초 업로드: 2025-08-04 18:07:31
최근 수정 시간: 2025-08-04 18:08:01
게시자: rlatjwls3333
카테고리: 백준
조회수: 10
# [Gold IV] The Tower of Babylon [문제 링크](https://www.acmicpc.net/problem/6599) ## 문제 설명 <p>Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole story:</p> <p>The babylonians had <em>n</em> types of blocks, and an unlimited supply of blocks of each type. Each type-<em>i</em> block was a rectangular solid with linear dimensions (<em>x<sub>i</sub>, y<sub>i</sub>, z<sub>i</sub></em>). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height.</p> <p>They wanted to construct the tallest tower possible by stacking blocks. The problem was that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked.</p> <p>Your job is to write a program that determines the height of the tallest tower the babylonians can build with a given set of blocks.</p> ## 입력 <p>The input file will contain one or more test cases. The first line of each test case contains an integer <em>n</em>, <span style="line-height:1.6em">representing the number of different blocks in the following data set. The maximum value for </span><em style="line-height:1.6em">n</em><span style="line-height:1.6em"> is 30.</span></p> <p>Each of the next <em>n</em> lines contains three integers representing the values <em>x<sub>i</sub></em>, <em>y<sub>i</sub></em> and <em>z<sub>i</sub></em>.</p> <p>Input is terminated by a value of zero (0) for <em>n</em>.</p> ## 출력 <p>For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format "Case <em>case</em>: maximum height = <em>height</em>"</p> ## 풀이 #### 각 상자를 어디를 높이를 둘 지에 따라 3개씩 나누었고, 각각의 상자에서 쌓을 수 있는 다른 상자로 간선을 만들어준 후 위상정렬로 최대 길이를 구했다. ``` c++ #include<bits/stdc++.h> using namespace std; struct block { int x, y, h; }; int main() { ios::sync_with_stdio(0); cin.tie(0); for(int tc=1;;tc++) { int n; cin >> n; if(!n) break; vector<block> blocks; for(int i=0;i<n;i++) { int x, y, z; cin >> x >> y >> z; blocks.push_back({x, y, z}); blocks.push_back({x, z, y}); blocks.push_back({y, z, x}); } n *= 3; vector<int> inBound(n); vector<vector<int>> conn(n); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(blocks[i].x>blocks[j].x && blocks[i].y>blocks[j].y || blocks[i].x>blocks[j].y && blocks[i].y>blocks[j].x) { conn[i].push_back(j); inBound[j]++; } } } queue<int> q; vector<long long> maxH(n); for(int i=0;i<n;i++) { if(inBound[i]==0) { q.push(i); maxH[i] = blocks[i].h; } } while(!q.empty()) { int cur = q.front(); q.pop(); for(int next : conn[cur]) { if(--inBound[next]==0) q.push(next); maxH[next] = max(maxH[next], maxH[cur]+blocks[next].h); } } cout << "Case " << tc << ": maximum height = " << *max_element(maxH.begin(), maxH.end()) << '\n'; } } ```