动态规划 - 杭电acm1069

http://acm.hdu.edu.cn/showproblem.php?pid=1069

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {

    static Scanner input = new Scanner(System.in);
    static {

    }
    static Map blocksMap;

    public static void main(String[] args) {
        int n;
        int index = 0;
        while ((n = Integer.parseInt(input.nextLine())) != 0) {
            index++;
            blocksMap = new HashMap<>();
            int height = 0;
            for (int i = 0; i < n; i++) {
                initBlock(input.nextLine());
            }

            List blocks = new ArrayList<>();
            blocks.addAll(blocksMap.values());
            Collections.sort(blocks);

            for (int i = 0; i < blocks.size(); i++) {
                Block current = blocks.get(i);
                if (current.best == 0) {
                    current.best = current.Height;
                }
                for (int j = i - 1; j >= 0; j--) {
                    Block tmp = blocks.get(j);
                    if (tmp.Length < current.Length && tmp.Width < current.Width && tmp.best + current.Height > current.best) {
                        current.best = tmp.best + current.Height;
                    }
                }
            }

            for (Block block : blocks) {
                if (block.best > height) {
                    height = block.best;
                }
            }

            System.out.println(String.format("Case %s: maximum height = %s", index, height));

        }
    }

    static int canAdd(List levels, int x, int y) {
        int result = -1;
        if (levels.size() == 0) {
            return 0;
        }
        return result;
    }

    static void initBlock(String str) {
        String[] strings = str.split(" ");
        int x = Integer.parseInt(strings[0]);
        int y = Integer.parseInt(strings[1]);
        int z = Integer.parseInt(strings[2]);
        addOrNot(x, y, z);
        addOrNot(x, z, y);

        addOrNot(y, x, z);
        addOrNot(y, z, x);

        addOrNot(z, x, y);
        addOrNot(z, y, x);
    }

    private static void addOrNot(int x, int y, int z) {
        if (!blocksMap.containsKey(x + "," + y) || (blocksMap.containsKey(x + "," + y) && blocksMap.get(x + "," + y).Height < z)) {
            blocksMap.put(x + "," + y, new Block(x, y, z));
        }
    }

    static class Block implements Comparable {

        public Block(int x, int y, int z) {
            this.Width = x;
            this.Length = y;
            this.Height = z;
        }

        public int Width, Length, Height;

        @Override
        public String toString() {
            return Width + "," + Length + "," + Height;
        }

        public int best = Height;

        @Override
        public int compareTo(Block o) {
            return Width == o.Width ? Length - o.Length : Width - o.Width;
        }

    }

}

你可能感兴趣的:(动态规划 - 杭电acm1069)