包围矩形的最小面积?

发布于 2024-12-13 14:11:52 字数 147 浏览 2 评论 0原文

我需要一个算法来解决以下问题(我已经尝试过一些个人的 解决方案,但它们似乎不是最佳的)

如果给定一个带有标记和未标记区域(以矩阵形式)和2个矩形的表面 您可以以任何形式或位置进行操作,找到可能的形状和位置 的矩形,使其覆盖所有标记区域,同时保持最小 可能的表面积。

I need an idea for an algorithm to solve the following problem (I already tried some personal
solutions but they don't seem to be optimal)

If given a surface with marked and unmarked zones (in matrix form), and 2 rectangles
that you can manipulate in any form or position, find the possible shape and position
of the rectangles such that they cover all the marked zones while keeping the minimum
surface area possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2024-12-20 14:11:52

这个答案假设您无法旋转矩形,并且边始终平行于 x 轴和 y 轴。

首先,找到包围整个区域的矩形。其算法如下(假设原点位于左上角):

For each marked spot in the matrix:
    if spot.x < rectangle.left:
        rectangle.left = spot.x
    if spot.x > rectangle.right:
        rectangle.left = spot.x
    if spot.y < rectangle.top:
        rectangle.left = spot.x
    if spot.y < rectangle.bottom:
        rectangle.left = spot.x

然后,找到最大的水平间隙,如下所示:

largest_gap = -1
For each column in matrix:
     last_marked_spot = 0, 0
     For each spot in column:
         if spot.marked:
             if spot.x - last_marked_spot.x > largest_gap:
                 largest_gap = spot.x - last_marked_spot.x
             last_marked_spot = spot

垂直间隙也是如此。然后检查哪个差距最大。

然后使用最大的间隙作为分隔符将包含所有的矩形分成两部分。最后一步是折叠两个矩形(使用与顶部算法相反的算法)。

This answer is assuming you can not rotate the rectangles and the sides are always parallel to the x and y axis.

First, find the rectangle that encloses the complete area. An algorithm for that goes like this (assuming the origin is at the topleft):

For each marked spot in the matrix:
    if spot.x < rectangle.left:
        rectangle.left = spot.x
    if spot.x > rectangle.right:
        rectangle.left = spot.x
    if spot.y < rectangle.top:
        rectangle.left = spot.x
    if spot.y < rectangle.bottom:
        rectangle.left = spot.x

Then, find the largest horizontal gap like this:

largest_gap = -1
For each column in matrix:
     last_marked_spot = 0, 0
     For each spot in column:
         if spot.marked:
             if spot.x - last_marked_spot.x > largest_gap:
                 largest_gap = spot.x - last_marked_spot.x
             last_marked_spot = spot

Same goes for vertical gap. Then check which gap is the biggest.

Then divide the all-including rectangle in two parts using the biggest gap as seperator. The final step is to collapse the two rectangles (using the reverse of the algorithm on the top).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文