给你一堆n个箱子,箱子宽wi、高hi、深di。箱子不能翻转,将箱子堆起来时,下面箱子的宽度、高度和深度必须大于上面的箱子。实现一个方法,撘出最高的一堆箱子,箱堆的高度为每个箱子高度的总和。
分析:用递归,子问题为以某一个箱子为底的高度最高的塔,确定底后,找到一个可以放在底上的箱子,再递归求解子问题。
注意:存在重复子问题,考虑用动态规划,用表记录中间结果,以便之后查询。
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);