Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将相同大小的矩形排列成更大矩形的算法_Java_Algorithm_Packing - Fatal编程技术网

Java 将相同大小的矩形排列成更大矩形的算法

Java 将相同大小的矩形排列成更大矩形的算法,java,algorithm,packing,Java,Algorithm,Packing,我有一个大小为W x H的矩形和一个较大的矩形,其边的长度是W和H的GCD的倍数 我正在尝试确定我可以放入较大矩形的较小矩形的最大数量。较小的矩形可以旋转。我也不在乎右上角有一个小小的空白 到目前为止,我提出了: // +-------------+ We have 3 potential sections to fill with rects. The // | C | inital calculation is for section A. If A doesn't //

我有一个大小为W x H的矩形和一个较大的矩形,其边的长度是W和H的GCD的倍数

我正在尝试确定我可以放入较大矩形的较小矩形的最大数量。较小的矩形可以旋转。我也不在乎右上角有一个小小的空白

到目前为止,我提出了:

// +-------------+ We have 3 potential sections to fill with rects. The
// |      C      | inital calculation is for section A. If A doesn't
// +-------+-----+ consume the entire area then we then try and fill the
// |       |     | other areas with the alternate rotation.
// |   A   |  B  |
// |       |     |
// +-------+-----+
//
int numWidthA = (int)Math.floor(width / rect[0]);
int numLengthA = (int)Math.floor(length / rect[1]);
int numWidthB = 0;
int numLengthB = 0;
int numWidthC = 0;
int numLengthC = 0;

double remainingWidth = width - (numWidthA * rect[0]);
double remainingLength = length - (numLengthA * rect[1]);

if(remainingWidth != 0) {
    double usedLength = numLengthA * rect[1];

    numWidthB = (int)Math.floor(remainingWidth / rect[1]);
    numLengthB = (int)Math.floor(usedLength / rect[0]);
}

if(remainingLength != 0) {
    numWidthC = (int)Math.floor(width / rect[1]);
    numLengthC = (int)Math.floor(remainingLength / rect[0]);
}

System.out.printf("Rotation: %.1f x %.1f%n", rect[0], rect[1]);
System.out.printf("A: %d x %d%n", numWidthA, numLengthA);
System.out.printf("B: %d x %d%n", numWidthB, numLengthB);
System.out.printf("C: %d x %d%n", numWidthC, numLengthC);

return (numWidthA * numLengthA) + (numWidthB * numLengthB) +
       (numWidthC * numLengthC);
如果初始部分A过于贪婪,则此操作失败。它一直占用太多的空间,以至于没有足够的空间在另一个部分进行交替旋转。也就是说,它为H/2留下空间,而不是H。如果它使用了所有可用的空间,那么没问题,就是在它使用几乎所有的空间的时候

我试图递归删除一个矩形,直到有足够的空间进行多次交替旋转,但由于双精度错误,我遇到了麻烦。所以我希望有另一种解决办法

我也不喜欢这个解决方案,因为它将算法从O1带到On


另一个问题是确定矩形的初始旋转,目前,我看到哪个轮换消耗的面积最大,并将该轮换用于A部分。

您需要最好的解决方案,还是在给定的时间内满足于一个好的解决方案?@Bathsheba我必须看到实际应用程序中的差异,看看它是否有明显的差异才能做出决定。我的直觉告诉我一个好的解决方案就足够了。起初,我会使用模拟退火方法。看,你也可以看看这个。看一看。这似乎是针对这个问题的最新研究成果。