实现一个方法,搭出最高的一堆箱子,箱堆的高度为每个箱子高度的总和

来源:互联网 发布:大数据用英语怎么说 编辑:程序博客网 时间:2024/05/19 03:44

public ArrayList<Box> createStackR(Box[] boxes,Box bottom)
{
int max_height=0;
ArrayList<Box> max_stack=null;
for(int i=0;i<boxes.length;i++)
{
if(boxes[i].canBeAbove(bottom))
{
ArrayList<Box> new_stack=createStackR(boxes,boxes[i]);
int new_height=stackHeight(new_stack);
if(new_height>max_height)
{
max_stack=new_stack;
max_height=new_height;
}
}
}
if(max_stack==null)
         max_stack=new ArrayList<Box>();
if(bottom!=null)
max_stack.add(0,bottom);//插入箱堆底部
return max_stack;
}


public ArrayList<Box> createStackDP(Box[] boxes,Box bottom,HashMap<Box,ArrayList<Box>> atack_map)
{
if(bottom!=null && stack_map.containKey(bottom))
{
return stack_map.get(bottom);
}
int max_height=0;
ArrayList<Box> max_stack=null;
for(int i=0;i<boxes.length;i++)
{
if(boxes[i].canBeAbove(bottom))
{
ArrayList<Box> new_stack=createStackDP(boxes,boxes[i],stack_map);
int new_height=stackHeight(new_stack);
if(new_height>max_height)
{
max_stack=new_stack;
max_height=new_height;
}
}
}
if(max_stack==null) max_stack=new ArrayList<Box>();
if(bottom!=null) max_stack.add(0,bottom);
stack_map.put(bottom,max_stack);
return (ArrayList<Box>) max_stack.clone();
}




0 0