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.
其实看这个数据范围就应该可以想到状压,但是还是有些无从下手的感觉,不知道如何去进行dp,看题解之后才明白。
先枚举所有的点对,也就是所有小矩形的情况,并将对应的面积求出,并且对于在这个矩形内可完全包含的点添加上,这几个点就是这个面积对应的状态。
然后对于每种状态,尝试加入每个小矩形并对面积进行更新。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
#define ll long long
#define CL(a) memset(a,0,sizeof(a))
#define maxn 35
#define mod 100000000
int n;
int dp[1<<16],state[1<<16],area[1<<16];
struct point
{
int x,y;
} a[20];
void solve()
{
int cnt=0;
for(int i=0; i<n; i++) //枚举所有的点对组合
{
for(int j=i+1; j<n; j++)
{
state[cnt]=(1<<i)|(1<<j);
for(int k=0; k<n; k++) //枚举其他所有点,如果这个点被包含在这个矩阵里,更新这个矩阵的状态
if((a[k].x-a[i].x)*(a[k].x-a[j].x)<=0&&(a[k].y-a[i].y)*(a[k].y-a[j].y)<=0)
state[cnt]|=1<<k;
if(a[i].x==a[j].x) area[cnt]=abs(a[i].y-a[j].y);
else if(a[i].y==a[j].y) area[cnt]=abs(a[i].x-a[j].x);
else area[cnt]=abs(a[i].y-a[j].y)*abs(a[i].x-a[j].x);
cnt++;
}
}
memset(dp,0x3f,sizeof(dp));
dp[0]=0;
for(int s=0; s<(1<<n); s++)
{
for(int k=0; k<cnt; k++)
dp[s|state[k]]=min(dp[s|state[k]],dp[s]+area[k]);
}
cout<<dp[(1<<n)-1]<<endl;
}
int main ()
{
while(cin>>n)
{
if(n==0) break;
for(int i=0; i<n; i++)
cin>>a[i].x>>a[i].y;
solve();
}
return 0;
}