用最合适的“块”填充一个区域

时间:2013-05-24 20:40:49

标签: python algorithm

我有一些代码可以根据用户的选择生成文本块。这些文本块的高度因用户选择的项目而异。我要做的是确保以最有效的方式在页面上排列这些块。

例如,第1部分高250点,第2部分高650点。如果用户选择:

表格中a部分的400分内容 来自b部分的200点内容 来自c部分的250个内容点 来自d部分的50个内容

我如何确保b部分和d部分进入第1部分和a部分& c进入第2节?

到目前为止,这是我的代码:

section1_height = 250
section2_height = 650

#list1 and list2 are the variables that contain the user selections
Column1 = DWIMBLOCK([list1], (18, 430), (LEFT, TOP_BASELINE), (250, 250))
Column2 = DWIMBLOCK([list2], (275, 430), (LEFT, TOP_BASELINE), (250, 650))

columns = [Column1, Column2]
sec1_columns = []
sec2_columns = []

for column in columns:
 if column.height <= 250:
  sec1_columns.append(column)

for shorts in sec1_columns:
 if #This is where I am stuck

正如你所看到的,我已经将我的专栏分成了不到250点高的专栏,但现在我被迫尝试按照if sum of any number of blocks <= 250, assign those blocks to a new list的方式做一些事情。我该怎样做呢?谢谢!

更新:

以下是布局的大致轮廓,以便您可以获得更清晰的画面。

____________________
|#########**********|
|# image #*        *|
|#########*        *|
|**********        *|
|*       **        *|
|*sec. 1 **        *|
|*       **sec. 2  *|
|**********        *|
|#########*        *|
|#       #*        *|
|# image #*        *|
|#       #*        *|
|#########**********|
____________________

这两张图片总是在同一个地方,大小相同 还应该注意,这是用于PDF生产,而不是Web使用,因此CSS和Javascript不是选项。我正在使用的环境仅允许Python代码。

1 个答案:

答案 0 :(得分:3)

基本上,这是一个接一个地解决每个部分的背包问题(长度为值和权重)。我将使用蛮力,但你可以查找并找到其他方法,这些方法在速度方面更有效,但可能无法提供最佳解决方案 - 这个方法确实如此。

填充第一部分的2^bb是块数)组合,因为对于每个块,您可以将其放在那里或不放在那里。只有其中一部分是可行的。你选择填充最多的组合。然后重复下一部分的剩余项目。

这应该让你知道如何做到这一点:

from itertools import combinations, chain

unassigned_blocks = {
    ('a', 400),
    ('b', 200),
    ('c', 250),
    ('d',  50),
    # ...
}

sections_and_assigned_blocks = {
    ('1', 250): {},
    ('2', 650): {},
    # ...
}

for section in sorted(sections_and_assigned_blocks.keys()):
    best, best_length = {}, 0
    for combination in chain(*[combinations(unassigned_blocks, n)
                               for n in xrange(1, len(unassigned_blocks)+1)]):
        combination = set(combination)
        length = sum(block[1] for block in combination)
        if best_length < length <= section[1]:
            best, best_length = combination, length
    sections_and_assigned_blocks[section] = best
    unassigned_blocks -= best

from pprint import pprint
pprint(sections_and_assigned_blocks)
# {('1', 250): set([('c', 250)]),
#  ('2', 650): set([('a', 400), ('b', 200), ('d', 50)])}

时间复杂度为O(s*2^b)s是部分的数量)。在最坏的情况下,1-3节太小而不能包含任何东西,将会有4 * 2 ^ 15 = 131072次迭代。在如此小的规模上,蛮力通常不是问题。但是,增加块数会对性能产生巨大影响!

相关问题