Java-检查是否为矩形;“适合”;进入二维阵列(二维装箱)
所以基本上我想创建一个方法,将2d数组作为1个参数,矩形的宽度和高度作为其他2个参数。2d数组只包含二进制数(0-空单元格,1-已取单元格)。如果容器中还有足够的位置容纳矩形,则该方法应返回true 我的主要问题是,我不知道如何在检查数组中是否确实存在rectHeight*rectWidth大小的空白时遍历2d数组 现在我只检查4个顶点是否可用,但显然这并不总是足够的Java-检查是否为矩形;“适合”;进入二维阵列(二维装箱),java,arrays,multidimensional-array,bin-packing,Java,Arrays,Multidimensional Array,Bin Packing,所以基本上我想创建一个方法,将2d数组作为1个参数,矩形的宽度和高度作为其他2个参数。2d数组只包含二进制数(0-空单元格,1-已取单元格)。如果容器中还有足够的位置容纳矩形,则该方法应返回true 我的主要问题是,我不知道如何在检查数组中是否确实存在rectHeight*rectWidth大小的空白时遍历2d数组 现在我只检查4个顶点是否可用,但显然这并不总是足够的 for (int j = y; j < y + rowHeight - rectHeight + 1; j++){
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);
}
}
}
for(int j=y;j
可以使用覆盖图,其中空单元格是顶点:
在两个循环中实现(每个循环使用单独的方法)会更容易:
//测试数据
专用静态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},
};
公共静态void main(字符串[]args){
System.out.println(isEnoughtSpace(1,1));//输出-true
System.out.println(isEnoughtSpace(2,2));//输出-true
System.out.println(isEnoughtSpace(3,3));//输出-true
System.out.println(isEnoughtSpace(1,3));//输出-true
System.out.println(isEnoughtSpace(3,1));//输出-true
System.out.println(isEnoughtSpace(4,1));//输出-false
System.out.println(isEnoughtSpace(4,5));//输出-false
System.out.println(isEnoughtSpace(11,11));//输出-false
System.out.println(isEnoughtSpace(0,0));//输出-true
}
私有静态布尔值isEnoughtSpace(int-rectHeight,int-recWidth){
for(int row=0;row Hint:编写一个函数,该函数接受一个2D数组、一对索引i和j以及一个宽度和高度对。当且仅当可以在该2D数组中的位置(i,j)放置一个宽度*高度矩形时,该函数才应返回true。顺便说一句,如果你真的关心时间复杂度,你就必须使用更聪明的东西。告诉我们你到目前为止都尝试了哪些代码。你到底不知道什么?我想你知道如何迭代二维数组,以及如何检查插槽是否为空。你到目前为止尝试了什么?你在寻找可行的解决方案吗在有效解决方案上或之后?(如果是后者,您已经对该主题进行了哪些研究?)我已经实现了按高度排序和填充算法,但在我的例子中,这还不够,因为我有一个固定的binwidth和binheight,因此该算法在大多数情况下都不会成功。因此,当该算法必须开始新行时,我想在新行开始之前检查是否有其他矩形的空间。
//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;
}