您的位置:首页 > Web前端 > AngularJS

poj 2836 Rectangular Covering(状态压缩dp)

2015-08-29 02:38 507 查看
Description

n points are given on the Cartesian plane. Now you have to use some rectangles whose sides are parallel to the axes to cover them. Every point must be covered. And a point can be covered by several rectangles. Each rectangle should cover at least two points including those that fall on its border. Rectangles should have integral dimensions. Degenerate cases (rectangles with zero area) are not allowed. How will you choose the rectangles so as to minimize the total area of them?


Input

The input consists of several test cases. Each test cases begins with a line containing a single integer n (2 ≤ n ≤ 15). Each of the next n lines contains two integers x, y (−1,000 ≤ x, y ≤ 1,000) giving the coordinates of a point. It is assumed that no two points are the same as each other. A single zero follows the last test case.


Output

Output the minimum total area of rectangles on a separate line for each test case.


Sample Input

2
0 1
1 0
0


Sample Output

1


Hint

The total area is calculated by adding up the areas of rectangles used.


Source

PKU Local 2006, kicc

先预处理数据,将n个点两两组合形成n * (n-1) / 2个矩形,计算每个矩形的面积和内部点个数。

接着利用预处理数据来枚举,定义

dp[S] := 矩形集为S时的最省面积

先假设平面上没有矩形,那么dp[0]=0,接着一个一个地往平面上加矩形,递推关系是:

dp[新矩形集合] = min(dp[新矩形集合], dp[旧矩形集合] + 新矩形的面积);

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define inf 1<<29
#define N 16
#define M 1<<N
int x
,y
;
int state

;
int area

;
int dp[M];
int main()
{
int n;
while(scanf("%d",&n)==1){
if(n==0)
break;
memset(state,0,sizeof(state));
for(int i=0;i<n;i++){
scanf("%d%d",&x[i],&y[i]);
}
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
int lx=min(x[i],x[j]),rx=max(x[i],x[j]);
int ly=min(y[i],y[j]),ry=max(y[i],y[j]);
for(int k=0;k<n;k++){
if(lx<=x[k] && x[k]<=rx && ly<=y[k] && y[k]<=ry){
state[i][j]+=1<<k;
}
}
int a=rx-lx?rx-lx:1;
int b=ry-ly?ry-ly:1;
area[i][j]=a*b;
}
}

for(int i=0;i<(1<<n);i++)
dp[i]=inf;
dp[0]=0;
for(int i=0;i<(1<<n);i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
int T= i|state[j][k];
dp[T]=min(dp[T],dp[i]+area[j][k]);
}
}
}
printf("%d\n",dp[(1<<n)-1]);
}
return 0;
}


View Code

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: