1

I have the following case:

  • An GridPane divided into cells, so f.e. 6 x 6 = 36 cells
  • A concrete number of Elements which need to fit into this area, f.e. 6
  • Each element has an area, f.e. {3, 6, 4, 5, 6, 12}, so that the total area is again the overall (36)

And here my question: how do I calculate how many possible options I have to:

  1. Calculate row / colums span (height / width) of the elements : for an area of 6, the different options could for example be 6x1, 1x6, 3x2 and 2x3 .. A necessary condition is that elements need to be a square or a rectangle, so f.e. not an U or T-shape occupying the grid in the cells. Here is where I'm struggeling! so should I do cases like if cases? I guess there is a more efficient way to do so! And which would be the conditions?

  2. Position those elements inside the area

I'm programming in java and javafx.

I tried the following approach so far for calculating span /height/width:

    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);
lydiaP
  • 123
  • 1
  • 13
  • You can use a `int` `row` as number of rows for a area of size `n` iff `n % row == 0`. The number of columns is `n / row` in this case. I guess your best option here is a depth first search for solutions (sorting out invalid results early on). – fabian May 04 '18 at 12:59
  • Hi Fabian, thanks! I updated the question and added some code - so yes, I also thought about modulo, but I don't know how once I get the solution which satisfy the criteria (modulo == 0) calculate the different options of the layout (f.e. 3 x 2, 2x3, 6x1... ). – lydiaP May 04 '18 at 13:07
  • Just use a loop to go through all possible values for row values and check, if they are valid. BTW: thinking about it for a short while you should probably determine the element of the "next free" cell instead of considering all positions for & dimensions for all elements. If you don't know about depth first search yet, take a look at backtracking and simply don't stop at a single solution. – fabian May 04 '18 at 13:16

1 Answers1

2

The elements array could be sorted with decreasing sizes.

As the entire area must be filled, one can fill from top to bottom, left to right.

For every area element, the possible forms must be iterated through; for 6: (1, 6), (6, 1), (2, 3), (3, 2): finding factor pairs.

Recursion with back-tracking; the recursion looks like:

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

I will not spoil the nice puzzling which lies ahead of you.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138