POJ 2836 Rectangular Covering 状态压缩DP

来源:互联网 发布:印度 网络空间作战部队 编辑:程序博客网 时间:2024/04/30 10:32
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.
/*AC*/#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int NS = 15;const int INF = 1e9;int g[NS][NS],ar[NS][NS];int x[NS],y[NS],dp[1<<NS];int main(){    int n;    while (~scanf("%d", &n) && n){        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++){ //枚举两个顶点确定的矩形的组合            g[i][j]=0; //g[i][j]表示序号为i和j的顶点所确定的矩形覆盖的顶点序号的集合            int lx=min(x[i],x[j]), rx=max(x[i],x[j]); //计算出矩形的边界            int dy=min(y[i],y[j]), uy=max(y[i],y[j]);            for (int k=0;k<n;k++)                if (lx<=x[k] && x[k]<=rx && dy<=y[k] && y[k]<=uy)                    g[i][j]|=1<<k; //将序号为k的顶点加入到集合中            int a = rx-lx? rx-lx:1; //对于两个点共线的情况的特殊处理            int b = uy-dy? uy-dy:1;            ar[i][j] = ar[j][i] = a*b; //覆盖序号为i和j为顶点的所需要的矩形面积        }        int MAXS = (1<<n)-1;        dp[0] = 0;        for (int S=1;S<=MAXS;S++)            dp[S] = INF;        for (int S=0;S<=MAXS;S++) //直到所有点都被覆盖            for (int j=0;j<n;j++)                for (int k=0;k<j;k++){                    int T = S | g[j][k];                    dp[T]=min(dp[T], dp[S]+ar[j][k]);        }        printf("%d\n",dp[MAXS]);    }    return 0;}

0 0
原创粉丝点击