我有以下情况:
- GridPane 分为多个单元格,因此 f.e. 6 x 6 = 36 个单元
- 需要适合该区域的具体元素数量,例如6
- 每个元素都有一个区域,例如{3, 6, 4, 5, 6, 12},这样总面积又是整体 (36)
这是我的问题:我如何计算我必须的有多少种可能的选择:
计算元素的行/列跨度(高度/宽度):对于 6 的区域,不同的选项可以是 6x1、1x6、3x2 和 2x3 等。必要条件是元素需要是正方形或矩形,所以 f.e.不是占据单元格中的 U 形或 T 形。这就是我挣扎的地方! 那么我应该像 if 案例那样做案例吗?我想有一个更有效的方法来做到这一点!条件是什么?
将这些元素放置在区域内
我正在使用 java 和 javafx 进行编程。
到目前为止,我尝试了以下方法来计算跨度/高度/宽度:
int NumberRow = 6;
int NumberColum = 6;
int columspan;
int rowspan;
int area = 6;
if (area / NumberRow == 1) {
columspan = 1;
rowspan = area;}
if (area/NumberColum== 1) {
columspan = area;
rowspan = 1;
}
// if area modulo NumberColum or NumberRow == 0 it's an multiple..
if (area % NumberColum == 0 ) {
???? --> what would make sense here ?
}
VBox v = new VBox();
v.getChildren().addAll(h1, t, image);
grid.add(v, colum, row, columspan, rowspan);
最佳答案
元素数组可以按大小递减进行排序。
由于必须填充整个区域,因此可以从上到下、从左到右填充。
对于每个区域元素,必须迭代可能的形式;对于 6:(1, 6)、(6, 1)、(2, 3)、(3, 2):查找因子对。
带回溯的递归;递归看起来像:
if (free space == 0)
print success with placements
++solution count
return;
for every candidate:
if (candidate fits here)
place candidate here
recurse (candidates without candidate)
remove candidate here
我不会破坏你面前的这个令人费解的谜题。
关于java - 一个区域内的排列数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50175229/