poj 2836 Rectangular Covering(状压DP)

Rectangular Covering
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1716 Accepted: 468

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 xy (−1,000 ≤ xy ≤ 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.


给n个点,用矩形覆盖,让所有的点都被覆盖,求覆盖的矩形面积最小。

先枚举n个点组成的n*(n-1)个矩形,算出它的面积,并记录矩形内的点,最后dp求将所有的点覆盖的最小面积。


代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int dp[1<<15],s[400],area[400];
struct node
{
    int x;
    int y;
}p[20];
int abss(int x)
{
    if(x<0)
    return -x;
    else
    return x;
}
int main()
{
    int n,cur;
    while(~scanf("%d",&n)&&n)
    {
        cur=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        memset(s,0,sizeof(s));
        for(int i=0;i<n;i++)//枚举所有的矩形,并算出面积
        {
            for(int j=i+1;j<n;j++)
            {
                s[cur]=(1<<i)|(1<<j);

                for(int k=0;k<n;k++ )
                {
                    if(k==i||k==j)
                    continue;
                    if((p[i].x-p[k].x)*(p[j].x-p[k].x)<=0&&(p[i].y-p[k].y)*(p[j].y-p[k].y)<=0)//判断k点在i,j组成的矩形内
                    {
                        s[cur]|=(1<<k);//将k点放入i,j组成的矩形内
                    }
                }
                //计算面积
                if(p[i].x==p[j].x)
                area[cur]=abss(p[i].y-p[j].y);
                else if(p[i].y==p[j].y)
                {
                    area[cur]=abss(p[i].x-p[j].x);
                }
                else
                {
                    area[cur]=abss(p[i].x-p[j].x)*abss(p[i].y-p[j].y);
                }


                cur++;
            }
        }
       // printf("jkds\n");
        dp[0]=0;
        for(int i=1;i<(1<<n);i++ )
        dp[i]=99999999;
        for(int i=0;i<(1<<n);i++)
        {
            for(int j=0;j<cur;j++)
            {
                dp[i|s[j]]=min(dp[i|s[j]],dp[i]+area[j]);//原来的选的点的集合为i,判断是否选第j个矩形,
            }
        }
        printf("%d\n",dp[(1<<n)-1]);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值