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 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个点组成的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; }
poj 2836 Rectangular Covering(状压DP)
原文地址:http://blog.csdn.net/caduca/article/details/40474309