/**
* 功能:给你一堆n个箱子,箱子宽wi,高hi,深di。箱子不能翻转,将箱子堆起来时,下面箱子的宽度、高度和深度必须大于上面的箱子。
* 实现方法:搭出最高的一堆箱子,箱子堆的高度为每个箱子高度的总和。
*/
两种方法:
方法一:递归法
[java] view plain copy
- //递归法
- public static ArrayList<Box> createStackR(Box[] boxes,Box bottom){
- int maxHeight=0;
- ArrayList<Box> maxStack=null;
- for(int i=0;i<boxes.length;i++){
- if(boxes[i].canBeAbove(bottom)){
- ArrayList<Box> newStack=createStackR(boxes,boxes[i]);
- int newHeight=stackHeight(newStack);
- if(newHeight>maxHeight){
- maxHeight=newHeight;
- maxStack=newStack;
- }
- }
- }
- if(maxStack==null)
- maxStack=new ArrayList<Box>();
- if(bottom!=null)
- maxStack.add(0,bottom);
- return maxStack;
- }
- public static int stackHeight(ArrayList<Box> stack){
- int height=0;
- for(int i=0;i<stack.size();i++){
- height+=stack.get(i).heigth;
- }
- return height;
- }
方法二:动态规划
[java] view plain copy
- //动态规划
- public static ArrayList<Box> createStackDP(Box[] boxes,Box bottem,HashMap<Box,ArrayList<Box>> stackMap){
- if(bottem!=null&&stackMap.containsKey(bottem))
- return stackMap.get(bottem);
- int maxHeight=0;
- ArrayList<Box> maxStack=null;
- for(int i=0;i<boxes.length;i++){
- if(boxes[i].canBeAbove(bottem)){
- ArrayList<Box> newStack=createStackDP(boxes, boxes[i], stackMap);
- int newHeight=stackHeight(newStack);
- if(newHeight>maxHeight){
- maxStack=newStack;
- maxHeight=newHeight;
- }
- }
- }
- if(maxStack==null)
- maxStack=new ArrayList<Box>();
- if(bottem!=null)
- maxStack.add(0, bottem);
- stackMap.put(bottem, maxStack);
- /**
- * 方法clone()来自Object类,其方法签名如下:重写方法时,可以调整参数,但不得改动返回类型。
- * 因此,如果继承自Object的类重写了clone()方法,它的clone()方法仍将返回Object实例。因此必须转型返回值。
- */
- return (ArrayList<Box>) maxStack.clone();//返回副本
- }
- lt;pre name="code" class="java"><pre name="code" class="java"> public static int stackHeight(ArrayList<Box> stack){
- int height=0;
- for(int i=0;i<stack.size();i++){
- height+=stack.get(i).heigth;
- }
- return height;
- }
箱子
- class Box{
- int width;
- int heigth;
- int depth;
- public Box(int width,int heigth,int depth){
- this.width=width;
- this.heigth=heigth;
- this.depth=depth;
- }
- public boolean canBeAbove(Box box){
- if(box.width>this.width&&box.heigth>this.heigth&&box.depth>this.depth)
- return true;
- return false;
- }
- }
或者:
分析:用递归,子问题为以某一个箱子为底的高度最高的塔,确定底后,找到一个可以放在底上的箱子,再递归求解子问题。
注意:存在重复子问题,考虑用动态规划,用表记录中间结果,以便之后查询。
[java] view plain copy
- package cci;
- import java.util.ArrayList;
- class Box{
- int height;
- int width;
- int depth;
- public Box(int height, int width, int depth){
- this.height=height;
- this.width=width;
- this.depth = depth;
- }
- public boolean canBeAbove(Box box){
- if(box==null)
- return true;
- if(height<box.height && width<box.width && depth<box.depth)
- return true;
- return false;
- }
- public void print(){
- System.out.println(height+" "+width+" "+depth);
- }
- }
- public class CCI_9_10 {
- public static ArrayList<Box> maxBoxTower(Box[] boxes){
- return maxBoxTower(boxes, null);
- }
- private static ArrayList<Box> maxBoxTower(Box[] boxes, Box bottom){
- ArrayList<Box> maxTower = new ArrayList<Box>();
- int maxHeight = 0;
- //尝试每一个箱子
- for(int i=0; i<boxes.length; i++){
- //找到可以放在bottom上的箱子
- if(boxes[i].canBeAbove(bottom)){
- //以找到的箱子为底求解子问题(注意,这里的子问题会被重复求解,提高效率的办法是动态规划)
- ArrayList<Box> newTower = maxBoxTower(boxes, boxes[i]);
- //利用子问题的解构造当前问题的解
- int newHeight = calTowerHeight(newTower);
- if(newHeight>maxHeight){
- maxHeight = newHeight;
- maxTower = newTower;//以boxes[i]为底的最高塔
- }
- }
- }
- if(bottom != null){
- maxTower.add(0, bottom);
- }
- return maxTower;
- }
- private static int calTowerHeight(ArrayList<Box> tower){
- int height=0;
- for(Box box : tower){
- height += box.height;
- }
- return height;
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Box[] boxes = new Box[3];
- boxes[0] = new Box(1,1,1);
- boxes[1] = new Box(2,2,2);
- boxes[2] = new Box(3,3,3);
- ArrayList<Box> result = maxBoxTower(boxes);
- for(Box item : result){
- item.print();
- }
- }
- }