fitting rectangles in the smallest possible area

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

IOI 95

\"basic

The six basic layouts of four rectangles

Four r

3条回答
  •  感情败类
    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.

提交回复
热议问题