Report: http://starforever.blog.hexun.com/2097115_d.html
Because there are only four squares, the order and placement direction (horizontal or vertical) of each square are enumerated. The placement method is only the six basic modes given by the question, calculate the minimum area in different modes and update the optimal solution.
4th and 5 are essentially the same ., The minimum area for different modes is as follows:
Set W1, W2, W3, and W4 to the horizontal length of the four blocks. H1, H2, H3, and H4 indicate the vertical length of the four blocks. W, h indicates the minimum value.
1: W = W1 + W2 + W3 + W4; H = max (H1, H2, H3, H4)
2: W = max (W1 + W2 + W3, W4); H = max (H1, H2, H3) + h4
3: W = max (W1 + W2, W3) + W4; H = max (H1 + H3, H2 + H3, H4)
4: W = W1 + W2 + max (W3, W4); H = max (H1, H3 + H4, H2)
5: H = max (H1 + H3, H2 + h4)
For W, we can divide it into the following four forms:
(1): h3> = h2 + H4; W = max (W1, W3 + W2, W3 + W4)
(2): h3> H4 and H3 <H2 + H4; W = max (W1 + W2, W2 + W3, W3 + W4)
(3): H4> H3 and H4 <H1 + H3; W = max (W1 + W2, W1 + W4, W3 + W4)
(4): H4> = H1 + H3; W = max (W2, W1 + W4, W3 + W4)
*: H3 = H4 (not shown in the figure); W = max (W1 + W2, W3 + W4)
Record the optimal solution with an array, and finally sort the output.