首页 > 专题系列 > Java解POJ > POJ 2241 The Tower of Babylon [解题报告] Java
2013
11-10

POJ 2241 The Tower of Babylon [解题报告] Java

The Tower of Babylon

问题描述 :

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.

输入:

The input 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.

输出:

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”

样例输入:

1
10 20 30
2
6 8 10
5 5 5
7
1 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

样例输出:

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

解题代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Scanner cin = new Scanner(System.in);

        int kase = 0;
        while (true) {

            int n = cin.nextInt();

            if (n == 0) {
                break;
            }

            kase++;

            List list = new ArrayList();

            for (int i = 1; i <= n; i++) {
                int x = cin.nextInt();
                int y = cin.nextInt();
                int z = cin.nextInt();

                list.add(new T(x, y, z));
                list.add(new T(y, x, z));

                list.add(new T(x, z, y));
                list.add(new T(z, x, y));

                list.add(new T(y, z, x));
                list.add(new T(z, y, x));

            }

            Collections.sort(list, new Comparator() {

                public int compare(Object arg0, Object arg1) {
                    T t1 = (T) arg0;
                    T t2 = (T) arg1;

                    if (t2.x > t1.x) {
                        return t2.x - t1.x;
                    } else if (t2.x == t1.x && t2.y > t1.y) {
                        return t2.y - t1.y;
                    } else if (t2.x == t1.x && t2.y == t1.y && t2.z > t1.z) {
                        return t2.z - t1.z;
                    }

                    return -1;
                }
            });

            int[] f = new int[list.size() + 1];
            for (int i = 0; i < f.length; i++) {
                f[i] = 0;
            }

            f[0] = list.get(0).z;

            int max = f[0];

            for (int i = 1; i < list.size(); i++) {
                int m = list.get(i).z;

                for (int k = 0; k < i; k++) {

                    if (list.get(k).x > list.get(i).x && list.get(k).y > list.get(i).y) {
                        m = Math.max(m, f[k] + list.get(i).z);
                    }
                }

                max = Math.max(max, m);

                f[i] = m;
            }
            System.out.println("Case " + kase + ": maximum height = " + max);
        }
    }
    
    static class T {

        int x;
        int y;
        int z;

        public T(int x, int y, int z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }

}

  1. Thanks for taking the time to examine this, I really feel strongly about it and love studying a lot more on this topic. If possible, as you acquire experience