【问题标题】:Algorithm design: inserting bags into containers with a limited size算法设计:将袋子插入大小有限的容器中
【发布时间】:2013-11-05 03:25:02
【问题描述】:

我必须创建一个算法,该算法需要将 n 个行李袋添加到 n 个容器中,每个容器可以容纳 50 公斤。每个袋子按顺序装入一个容器中。

行李袋重量示例字符串如下(每个数字代表一个袋子的重量):

16 24 25 3 20 18 7 17 4 15 13 22 2 12 10 5 8 1 11 21 19 6 23 9 14

集装箱装行李有两条规则:

  1. 一个集装箱可携带不超过 50 (kg) 的行李
  2. 如果下一个(空载的)袋子会导致容器超重,请将其放入下一个容器中

我的最终目标是打印每个集装箱的行李重量列表。行李袋示例字符串的示例输出为:

Container 1:  16  24
Container 2:  25  3  20 
Container 3:  18  7  17  4
Container 4:  15  13 22
Container 5:  2   12 10  5  8  1  11  
Container 6:  21  19 6 
Container 7:  23  9  14

我当前的代码无法创建容器,我现在正在寻找更好的方法来做到这一点。

public static void insertBagsContainer() {
    ArrayList<ArrayList<Integer>> containerArray = new ArrayList<ArrayList<Integer>>();
    int tempSum = 0;
    int x=0;

    for(int i=0; i<bags.size()-1; i++){
        tempSum = 0;
        ArrayList<Integer> innerBags = new ArrayList<Integer>();
        while (tempSum<= containerWeight){
            tempSum+= bags.get(x);
            innerBags.add(bags.get(x));
            x++;
        }
        containerArray.add(innerBags);
    }
}

【问题讨论】:

  • 您可能有兴趣知道您的任务是对Knapsack Problem 实施一个简单(幼稚、贪婪)的解决方案。
  • 每次拟合最小部分(重量小于剩余可用重量),达到整体最大拟合 - 这是这样做的贪婪方式

标签: java arrays algorithm sorting


【解决方案1】:

使用迭代器的经典示例。

public static void main(String[] args) {
    int maxWeight = 50;

    ArrayList<Integer> containerWeights = new ArrayList<Integer>();
    Integer[] weights = new Integer[] { 16, 24, 25, 3, 20, 18, 7, 17, 4, 15, 13, 22, 2, 12, 10, 5, 8, 1, 11, 21, 19, 6, 23, 9, 14 };

    Iterator<Integer> itr = Arrays.asList(weights).iterator();
    int current = itr.next(); //Get the first weight
    int containerWeight = 0;

    while(itr.hasNext()) {
        if(containerWeight + current > maxWeight) {
            containerWeights.add(containerWeight);
            containerWeight = current;
        } else {
            containerWeight += current;
        }
        current = itr.next();
    }
    containerWeights.add(current);
    System.out.println(Arrays.deepToString(containerWeights.toArray()));
}

打印: [40、48、46、50、49、46、14]

【讨论】:

  • 这仅在最后一次计算小于最大重量时才有效。给读者的一个练习……如果你将数组中的最后一个数字设为 45,会发生什么?您如何解决上述问题?
【解决方案2】:

我建议创建一个Container 类,该类有两个字段:List&lt;Integer&gt; containerint currentWeight,然后是一个boolean add(Integer luggage),如果是否插入了行李,则相应地返回一个boolean 值。然后,您可以拥有一个List&lt;Container&gt; containers,它会根据行李是否可以插入而相应增长。

在代码中:

class Container {
    private static final int MAX_SIZE = 50;
    private List<Integer> container;
    private int currentWeight;
    //luggage should be of type Luggage as well, just using Integer for sample purposes
    public boolean add(Integer luggage) {
        //implement it accordingly...
    }
}

class Bags {
    List<Container> containerList;
    //again, it should be List<Luggage>, just for sample purposes
    public void process(List<Integer> luggage) {
        //implement accordingly...
    }
}

实施细节由您决定。

【讨论】:

    【解决方案3】:

    只需考虑每个innerbags 列出一个容器。

    要打印出容器 1,您需要在 containerArray.get(0) 中打印出行李清单。

    听起来像是家庭作业,所以看看这是否能让你开始......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 2014-03-02
      • 1970-01-01
      • 2020-07-14
      相关资源
      最近更新 更多