简体   繁体   English

Java - 检查矩形是否“适合”二维数组(二维装箱)

[英]Java - Check if a rectangle "fits" into a 2 dimensional array (2d bin packing)

So basically I want to create a method that takes a 2d array as 1 parameter and a rectangle's width and height as the other 2 parameters.所以基本上我想创建一个方法,它将一个二维数组作为 1 个参数,一个矩形的宽度和高度作为其他 2 个参数。 The 2d array only contains binary numbers (0 - empty cell, 1 - taken cell).二维数组仅包含二进制数(0 - 空单元格,1 - 取单元格)。 The method should return true if there is still enough place in the bin to hold the rectangle.如果 bin 中仍有足够的空间来容纳矩形,则该方法应返回 true。

My main problem is that i don't know how to iterate through the 2d array while checking if there is in fact a rectHeight*rectWidth sized empty space in the array.我的主要问题是我不知道如何遍历二维数组,同时检查数组中是否确实存在 rectHeight*rectWidth 大小的空白空间。

Right now i'm only checking if the 4 vertices are available, but that obviously isn't always enough.现在我只检查 4 个顶点是否可用,但这显然并不总是足够的。

for (int j = y; j < y + rowHeight - rectHeight + 1; j++){
    for (int k = 0; k < bin[j].length - rectWidth + 1; k++){
        if (bin[j][k] == 0 && bin[j][k + rectWidth - 1] == 0 && bin[j + rectHeight - 1][k] == 0 && bin[j + rectHeight - 1][k + rectWidth - 1] == 0){
            isOne = true;
            for (int l = j; l < j + rectHeight; l++){
                for (int m = k; m < k + rectWidth; m++){
                    bin[l][m] = not_positioned.get(i).getId();
                }
            }
            not_positioned.remove(i);
        }
    }
}

It would be easier to implement in two loops (each in a separate methods) :在两个循环中实现会更容易(每个循环在一个单独的方法中):

//test data 
private static int[][] bin = {
            {1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,0,0,0,1,1,1},
            {1,1,1,1,0,0,0,1,1,1},
            {1,1,1,1,0,0,0,1,1,1},
            {1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1},
          };

public static void main(String[] args) {

        System.out.println(isEnoughtSpace(1, 1));// output - true
        System.out.println(isEnoughtSpace(2, 2));// output - true
        System.out.println(isEnoughtSpace(3, 3));// output - true
        System.out.println(isEnoughtSpace(1, 3));// output - true
        System.out.println(isEnoughtSpace(3, 1));// output - true
        System.out.println(isEnoughtSpace(4, 1));// output - false
        System.out.println(isEnoughtSpace(4, 5));// output - false
        System.out.println(isEnoughtSpace(11,11));// output - false
        System.out.println(isEnoughtSpace(0,0));// output - true
    }

    private static boolean isEnoughtSpace(int rectHeight, int recWidth) {

        for(int row = 0; row <= (bin.length - rectHeight); row++) {

            for(int col = 0; col <= (bin[0].length - recWidth); col++) {

                if(isEnoughtSpace(row, col, rectHeight, recWidth)) {
                    return true;
                }
            }
        }
        return false;
    }

    private static boolean isEnoughtSpace(int rowIndex, int colIndex,int rectHeight, int recWidth) {

        for(int row = rowIndex; row < (rowIndex+rectHeight) ; row ++) {

            for(int col = colIndex; col < (colIndex+recWidth) ; col++) {
                if(bin[row][col] == 1 ) {
                    return false;
                }
            }
        }
        return true;
    }

You may want to add some validity checks (like positive width and height).您可能想要添加一些有效性检查(如正宽度和高度)。

You could use a covering graph where empty cells are the vertices :您可以使用覆盖图,其中空单元格是顶点:

https://en.wikipedia.org/wiki/Covering_graph https://en.wikipedia.org/wiki/Covering_graph

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM