1. 程式人生 > >演算法競賽入門經典9.2例題9-2 巴比倫塔

演算法競賽入門經典9.2例題9-2 巴比倫塔

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: The babylonians had n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi; yi; zi). 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. 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. 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.

Input The input file will contain one or more test cases. The first line of each test case contains an integer n, representing the number of different blocks in the following data set. The maximum value for n is 30. Each of the next n lines contains three integers representing the values xi, yi and zi. Input is terminated by a value of zero (0) for n.

Output 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 case: maximum height = height’

Sample Input 1 10 20 30 26 8 10 5 5 5 71 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 5 31 41 59 26 53 58 97 93 23 84 62 64 33 83 27 0

Sample Output Case 1: maximum height = 40 Case 2: maximum height = 21 Case 3: maximum height = 28 Case 4: maximum height = 342


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
using namespace std;

struct Node {
	int x, y, h;
};

struct Cube {
	int x, y, z;
};

const int MAX_N = 31;


int n;
Cube cubes[MAX_N];
Node nodes[MAX_N * 3];
int G[MAX_N * 3][MAX_N * 3];
int d[MAX_N * 3]; // d[i] = length of longest path starting from node i


int dp(int i) { // i is node idx
	if (d[i] > 0) {
		return d[i];
	}
	
	d[i] = nodes[i].h;
	for (int j = 0; j < n * 3; j++) {
		if (G[i][j]) {
			d[i] = max(d[i], dp(j) + nodes[i].h);
		}
	}
	return d[i];
}

int main() {
	freopen("data.in", "r", stdin);
	freopen("data.out", "w", stdout);

	int kase = 0;
	while (cin >> n && n) {
		memset(cubes, -1, sizeof(cubes));
		memset(nodes, -1, sizeof(nodes));
		memset(G, 0, sizeof(G));
		memset(d, -1, sizeof(d));

		for (int i = 0; i < n; i++) {
			cin >> cubes[i].x >> cubes[i].y >> cubes[i].z;
		}

		// construct node
		for (int i = 0; i < n; i++) {
			nodes[i * 3].x = cubes[i].x, nodes[i * 3].y = cubes[i].y, nodes[i * 3].h = cubes[i].z;
			nodes[i * 3+1].x = cubes[i].y, nodes[i * 3+1].y = cubes[i].z, nodes[i * 3+1].h = cubes[i].x;
			nodes[i * 3 + 2].x = cubes[i].x, nodes[i * 3+2].y = cubes[i].z, nodes[i * 3+2].h = cubes[i].y;
		}

		// construct Graph
		for (int i = 0; i < n * 3; i++) {
			for (int j = 0; j < n * 3; j++) {
				if ((nodes[i].x > nodes[j].x && nodes[i].y > nodes[j].y) || (nodes[i].y > nodes[j].x && nodes[i].x > nodes[j].y)) {
					G[i][j] = 1;
				}
			}
		}
		//dp
		int max_h = 0;
		for (int i = 0; i < n * 3; i++) {
			if (dp(i) > max_h) {
				max_h = d[i];
			}
		}

		cout << "Case " << ++kase << ":" << max_h << endl;
	}
	return 0;
}