fitting rectangles in the smallest possible area

前端 未结 3 1828
心在旅途
心在旅途 2021-02-14 16:09

IOI 95

\"basic

The six basic layouts of four rectangles

Four r

相关标签:
3条回答
  • 2021-02-14 16:50

    While google does turn up some solutions, I guess some high level description will enable you to solve this on your own.

    You can start by naming the rectangles in each of the 6 layout cases 1,2,3,4. Then you should be able to calculate the bounding box for each of the layouts for given instances of rectangles 1...4 (hint for the first case: width=sum of widths of 1...4, height=max of heigths of 1...4)

    Then, as you said, you can try all possible combinations of naming four given rectangles with the indices 1...4 plus for each such possibility try out all possible rotations, and determine the minimum over all such possibilities in all layout cases.

    0 讨论(0)
  • 2021-02-14 16:58

    basically all the 24*16 combination of the given rectangle can be derived from the above 6 diagrams shown by taking reflection and rotation into account and even the permutation of the rectangles

    0 讨论(0)
  • 2021-02-14 17:00

    Since there are only four rectangles, enumeration of all possible layouts should work. Of course really all possible layouts is too much, but we can merge equivalent layouts.

    Let's consider only layouts where each rectangle cannot be moved left and up. This means that its upper border touches something and its left border touches something. All other layouts may be converted into such subset without making bounding box bigger.

    When we choose the first rectangle (one of four), it can touch only left and top borders of bounding box, so we have only one possible position for its left-top vertex.

    When we choose the second rectangle (one of three), its top border may touch either top border of the bounding box or bottom border of the first rectangle - 2 options. Similarly its left border may touch either left border of the bounding box or right border of the first rectangle - 2 more options. Thus we get 2x2=4 options for coordinate of its left-top vertex.

    And so on for third and fourth rectangle.

    Of course we should check at each step that there is no intersection between rectangles (e.g. if both of them will start from left-top corner of the bounding box). Also we sometimes will get obviously non-optimal layouts (e.g. when left-top vertex of the second rectangle coincides with right-bottom vertex of the first one), but that will not hurt result and can be removed if the solution is not fast enough.

    0 讨论(0)
提交回复
热议问题