Any algorithm to fill the gap with the least number of boxes

Let a 3D grid, like a chessboard, have an extra dimension. Now let's say that I have a certain number of cubes in this grid, each cube occupies 1x1x1 cells. Let them say that each of these cubes is an element.

What I would like to do is replace / merge these cubes into larger boxes, occupying any number of cells along the X, Y and Z axes, so that the resulting number of boxes is as small as possible, while preserving the overall appearance. "

This is probably unclear, so I will give a 2D example. Say I have a 2D grid containing several squares occupying 1x1 cells. The letter represents the cells occupied by this element, and each element has a different letter from the others. In the first example, we have 10 different elements, each of which occupies 1x1x1 cells.

+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
|   | A | B | C | D |   |
+---+---+---+---+---+---+
|   | E | F | G | H |   |
+---+---+---+---+---+---+
|   |   | K | L |   |   |
+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+

This is my input. Now I can optimize it, i.e. Reduce the number of elements while still occupying the same cells in several possible ways, one of which might be:

+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
|   | A | B | B | C |   |
+---+---+---+---+---+---+
|   | A | B | B | C |   |
+---+---+---+---+---+---+
|   |   | B | B |   |   |
+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+

Here, instead of 10 elements, I have only 3 (i, A, B and C). However, it can be optimized even more:

+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
|   | A | A | A | A |   |
+---+---+---+---+---+---+
|   | A | A | A | A |   |
+---+---+---+---+---+---+
|   |   | B | B |   |   |
+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+

Here I have only two elements: A and B. It is as optimized as possible.

, , - , , , , , 3D!

? , , , . !

+4
2

, , .

Min       x1 + x2 + x3 + ... //where x1 is 1 if the 1th partition is chosen, 0 otherwise
such that x1 +    + x3 = 1// if 1st and 3rd partition contain 1st item
               x2 + x3 = 1//if 2nd and 3rd partition contain 2nd item and so on.

          x1, x2, x3,... are binary

1 . , . .

NP- .

. - , , . , , , 2d 3d, "" ..

, " ".

+2

1(. 3), , . ( 2d. 3d, , , ?)

:

 ________
|        |
|_x....x_|
  |____|

x . , .

, , , x x. , a b, . , , ( ):

 ____________
|            |
|            |x
|            . |
|            . |a
|___         .   |
   b|        .   |
    |        .___|
    |________|x

4 :

 ____________
|            |
|            |x
|            . |
|            ..|a
|___..........   |
   b|        .   |
    |        .___|
    |________|x

- , , (x,x) (y,y):

 ____________
|            |
|            |x_
|            .  |
|            .  |
|___ . . . .z. .|y
   y|        .    |
    |        .____|
    |________|x

, , :

(y,z) (z,y) (x,z) (z,x)

4 .

, " " , .

< > 1. - , ( 26 2009 .)

+2

Source: https://habr.com/ru/post/1694734/


All Articles