0

我正在尝试使用 Next Fit 在线实现装箱算法。所以我事先不知道盒子的大小。无论如何,我有一个具有高度和宽度的 Box 类。一个列类,它表示和堆栈的盒子和一个卡车类,它保存着一堆盒子和一个布尔值,如果卡车是满的。

然而,当我在我的测试课上时,我会生成一个盒子列表和一个卡车列表来存放这些盒子。我将盒子添加到每辆卡车,当卡车几乎没有空间时,最后一个盒子尝试添加,但它不适合,所以布尔值 isFull 设置为 true,但进入该方法的盒子是然后迷路了。如何将盒子添加到卡车上,如果它已满,在下一辆卡车的呼叫中使用同一个盒子?

非常感谢

代码

public void addBoxesNextFit(Box b) 
{   

    if(truck.isEmpty())      // easy first case
    {
        *make a new column*
        *add box to column and add column to truck*
    }
    else  
    {
        *Get the last column in list*

        if(remainingHeight in column > BoxesHeight && widthOftheColumn > boxesWidth)
        {
            *add box to column*
            *add column to truck*                               
            boxesInTruck++;                             
        }
        else if(there is enough space to make another column with the box)
        {
            *Make new column*   
            *add box to column and column to truck*        
            boxesInTruck++;                    
            widthofAllColumns += b.getWidth(); //update width of all columns
        }
        else
        {
            truckFull = true; // if u can't add the box to 1 of the columns and you can't create a new column
        }                     // then the truck is full
    }
}
4

0 回答 0