Packing Rectangles

Problem:

Packing Rectangles
IOI 95
pack
The six basic layouts of four rectangles

Four rectangles are given. Find the smallest enclosing (new) rectangle into which these four may be fitted without overlapping. By smallest rectangle, we mean the one with the smallest area.

All four rectangles should have their sides parallel to the corresponding sides of the enclosing rectangle. Figure 1 shows six ways to fit four rectangles together. These six are the only possible basic layouts, since any other layout can be obtained from a basic layout by rotation or reflection. Rectangles may be rotated 90 degrees during packing.

There may exist several different enclosing rectangles fulfilling the requirements, all with the same area. You must produce all such enclosing rectangles.

PROGRAM NAME: packrec

INPUT FORMAT

Four lines, each containing two positive space-separated integers that represent the lengths of a rectangle's two sides. Each side of a rectangle is at least 1 and at most 50.

SAMPLE INPUT (file packrec.in)

1 2
2 3
3 4
4 5

OUTPUT FORMAT

The output file contains one line more than the number of solutions. The first line contains a single integer: the minimum area of the enclosing rectangles. Each of the following lines contains one solution described by two numbers p and q with p<=q. These lines must be sorted in ascending order of p, and must all be different.

SAMPLE OUTPUT (file packrec.out)

40
4 10
5 8

 

 


My Answer:


My Gain:

一开始发现题目给出的第四和第五种pack方式其实是一回事。
接着完全按照图上的四个矩形,编号i,j,k,l,边画图边写程序。
写好后发现,情况不完全。
后来在别人的博客里看到他使用了旋转,就想到,情况不完全无非是因为可以横着放也可以竖着放。
假如横的状态为1,竖的状态为0,很显然,四个矩形有2^4种情况,可由二进制进行演绎。
这样一来,作16次循环检验,就可以包括所有情况了,甚至还有重复。
另外需要注意的是,每次循环检验后,要记得还原四个矩形。

### 矩形打包算法及其解决方案 #### 贪婪离线算法 贪婪离线算法是一种常见的处理矩形打包的方法。该方法预先知道所有的矩形尺寸,在放置过程中总是尝试找到当前最优解,即最小化剩余空间的最大高度或宽度[^1]。 ```python def greedy_offline_packing(rectangles, bin_width): rectangles.sort(key=lambda r: (r['width'] * r['height']), reverse=True) bins = [] current_bin = {'rectangles': [], 'used_space': 0} for rect in rectangles: if current_bin['used_space'] + rect['width'] * rect['height'] <= bin_width ** 2: current_bin['rectangles'].append(rect) current_bin['used_space'] += rect['width'] * rect['height'] else: bins.append(current_bin) current_bin = {'rectangles': [rect], 'used_space': rect['width'] * rect['height']} bins.append(current_bin) return bins ``` 此代码实现了一个简单的基于面积的贪心策略来分配矩形到尽可能少的数量容器中去。 #### Skyline启发式算法 Skyline 启发式算法通过维护一个表示已填充区域顶部边界的skyline来进行工作。每当一个新的矩形被加入时,会寻找最适合它的位置并更新skyline以反映新的布局情况。 ```python class Rectangle(object): def __init__(self, width, height): self.width = width self.height = height def skyline_heuristic(rectangles): sorted_rects = sorted(rectangles, key=lambda x: (-x.width*x.height)) sky_lines = [(0, 0)] # (position, height) packed_boxes = [] for box in sorted_rects: best_fit_index = None min_waste_height = float('inf') for i in range(len(sky_lines)): waste_h = max(0, box.height - (sky_lines[i][1]-sky_lines[(i or len(sky_lines))-1][1])) if waste_h < min_waste_height and \ all((j >= len(sky_lines) or sk[0]>=box.width+sky_lines[i][0]) for j,sk in enumerate(sky_lines[i:], start=i)) : min_waste_height=waste_h;best_fit_index=i new_sky_line_point=(sky_lines[best_fit_index][0]+box.width,max(box.height+sky_lines[best_fit_index][1], sky_lines[-1][-1])) packed_boxes.append(((sky_lines[best_fit_index]),new_sky_line_point)) while len(sky_lines)>best_fit_index+1 and sky_lines[best_fit_index+1][1]<=new_sky_line_point[1]: del sky_lines[best_fit_index+1] sky_lines.insert(best_fit_index+1,new_sky_line_point) return packed_boxes ``` 这段Python代码实现了Skyline启发式的矩形打包逻辑,它试图优化空间利用率的同时保持较低的时间复杂度。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值