简体   繁体   中英

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. The 2d array only contains binary numbers (0 - empty cell, 1 - taken cell). The method should return true if there is still enough place in the bin to hold the rectangle.

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.

Right now i'm only checking if the 4 vertices are available, but that obviously isn't always enough.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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