文章标题 POJ : Rectangular Covering(状压DP)

4 篇文章 0 订阅

Rectangular Covering

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.

题意:在二维平面上有n个点,然后要我们选取与坐标轴平行的矩形将这n个点覆盖,每个矩形至少要覆盖上两个点,然后要我们求出覆盖所有的点所需要的矩形的面积和最小是多少。
分析:由于点的数目只有15,所以可以用状态压缩,并且先预处理出任意两点所形成的矩形,记录这两个点所形成的矩形的面积和所覆盖的点对应的状态,然后对于当前的这个矩形 所对应的面积为area,所对应的状态为s,则dp[i|s]=min(dp[i|s],dp[i]+area).
代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
typedef long long ll;

const int mod=1e9+7;
const int maxn=20;

int n;
struct node {
    int x,y;
}p[maxn];
int s[maxn*maxn];
int area[maxn*maxn];
int dp[1<<15];

int main()
{
    while (scanf ("%d",&n)&&n){
        for (int i=0;i<n;i++){
            scanf ("%d%d",&p[i].x,&p[i].y);
        }
        for (int i=0;i<(1<<n);i++)dp[i]=1e9;
        int cnt=0;
        for (int i=0;i<n;i++){
            for (int j=i+1;j<n;j++){
                s[cnt]=(1<<i)|(1<<j);//对应的状态,area表示对应的面积 
                for (int k=0;k<n;k++){//枚举点看有哪些点在矩形上 
                    if ((p[k].x-p[i].x)*(p[k].x-p[j].x)<=0&&(p[k].y-p[i].y)*(p[k].y-p[j].y)<=0)
                        s[cnt]=s[cnt]|(1<<k);//更新状态 
                }
                if (p[i].x==p[j].x)area[cnt]=fabs(p[i].y-p[j].y);//两个点平行y轴 
                else if (p[i].y==p[j].y)area[cnt]=fabs(p[i].x-p[j].x);//平行x轴 
                else area[cnt]=(fabs(p[i].x-p[j].x))*(fabs(p[i].y-p[j].y));
                cnt++;
            }
        }
        dp[0]=0;
        for (int i=0;i<(1<<n);i++){
            for (int j=0;j<cnt;j++){
                dp[i|(s[j])]=min(dp[i|s[j]],dp[i]+area[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、付费专栏及课程。

余额充值