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;
}