POJ 2836 Rectangular Covering 状态压缩DP 几何

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.

因为要面积最小,所以每个矩形的一条对角线的两个顶点肯定是这些点,所以可以枚举出以i,j为顶点的矩形可以覆盖的集合,以及它的面积。
此题不可以出现面积为0的矩形,所以当出现x[i]==x[j]或者y[i]==y[j]时,让它为0的这边的边长变成1。
预处理,枚举任意两点,求它的面积,并看这两点之间还有多少点被覆盖,将被覆盖的所有点存入状态。
然后就可以DP了

代码如下

/*AC*/
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

const int inf = 1e9;
int n, m;
int x[15], y[15];
int dp[1<<15];
int state[15*14/2], squ[15*14/2];

void Init(){ //预处理所有只包含对角线上两个点的状态及其面积
    m = 0;
    for(int i=0; i<n-1; i++){
        for(int j=i+1; j<n; j++){
            state[m] = 0;
            for(int k=0; k<n; k++){
                  if((x[i]-x[k])*(x[k]-x[j])>=0 && (y[i]-y[k])*(y[k]-y[j])>=0){ //用乘积符号判断点在矩形内,避免了比较对角线上两个点的大小
                    state[m] |= (1<<k);
                }
            }
            if(x[i] == x[j]) squ[m] = abs(y[i]-y[j]); //对角线的两个端点共线的情况
            else if(y[i] == y[j]) squ[m] = abs(x[i]-x[j]);
            else squ[m] = abs(x[i]-x[j])*abs(y[i]-y[j]);
            m++;
        }
    }
}

int main(){
    while(scanf("%d", &n) && n){
        for(int i=0; i<n; i++)
            scanf("%d %d", &x[i], &y[i]);
        Init();
        dp[0] = 0;
        for(int S=1; S<(1<<n); S++)
            dp[S] = inf;
        for(int S=0; S<(1<<n); S++){
            for(int i=0; i<m; i++){
                int T = S|state[i];
                if(T == S) continue; //如果state[i]是S的子集
                dp[T] = min(dp[T], dp[S]+squ[i]);
            }
        }
        printf("%d\n", dp[(1<<n)-1]);
    }
    return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值