我有不同尺寸的小矩形(1cm x 2xm,2cmx3cm,4cm * 6cm等)。不同類型矩形的數量可能因情況而異。每種類型的不同矩形可能具有不同數量的計數。最小面積計算算法(僅在邊緣放置瓷磚)
我需要創建一個包含所有這些小矩形的大矩形,這些小矩形只能放在的邊上。 不旋轉。理想的最終外矩形應該是方形的。 X〜Y。並非所有的邊緣都需要填滿。小矩形之間可能有間隙。圖片示例:
http://i.stack.imgur.com/GqI5z.png
我試圖編寫一個代碼來查找可以形成的最小可能區域。
我有一個循環遍歷所有可能的位置的算法來找出可能的最小區域。但是,隨着不同類型矩形的數量和矩形數量的增加,這需要很長時間。即2種類型的矩形,每種具有100 +矩形。 8 for循環。這將是〜100^8迭代
關於更好更快的算法來計算最小可能區域的任何想法?代碼是在Python中,但任何算法的概念都很好。
for rectange_1_top_count in (range(0,all_rectangles[1]["count"]+1)):
for rectange_1_bottom_count in range(0,all_rectangles[1]["count"]-rectange_1_top_count+1):
for rectange_1_left_count in (range(0,all_rectangles[1]["count"]-rectange_1_top_count-rectange_1_bottom_count+1)):
for rectange_1_right_count in ([all_rectangles[1]["count"]-rectange_1_top_count-rectange_1_bottom_count-rectange_1_left_count]):
for rectange_2_top_count in (range(0,all_rectangles[2]["count"]+1)):
for rectange_2_bottom_count in (range(0,all_rectangles[2]["count"]-rectange_2_top_count+1)):
for rectange_2_left_count in (range(0,all_rectangles[2]["count"]-rectange_2_bottom_count-rectange_2_top_count+1)):
for rectange_2_right_count in [(all_rectangles[2]["count"]-rectange_2_bottom_count-rectange_2_left_count-rectange_2_top_count)]:
area=calculate_minimum_area()
if area< minimum_area:
minimum_area=area
所以給出了外部矩形的大小,並且想要最小化中間的白色區域? –
使它變硬的條件是矩形只能放在邊緣/邊上。它們不能堆疊 –
沒有給出外部矩形的大小。只給出小矩形紀念。外部矩形的尺寸將隨着位置變化而改變。但是我想要在邊緣上放置最佳的小矩形,這樣可以提供最小的外矩形區域。 –