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
【分析】
这tm神题…虽然和NOIP2016 愤怒小鸟基本一样,但是满满坑点…调了两个小时,大家一定注意初始化。【angry birds传送门】
【代码】
//poj 2836 Rectangular Covering
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
int dp[1<<18],x[20],y[20],s[20][20],f[20][20]; //f[i][j]表示在ij被点覆盖时还能覆盖哪些点(01串)
bool can[20][20];
int n,m;
inline void init()
{
int i,j,k;
memset(s,0,sizeof s);
memset(f,0,sizeof f);
// memset(can,0,sizeof can);
memset(dp,0x3f,sizeof dp);
fo(i,0,n-1)
scanf("%d%d",&x[i],&y[i]);
fo(i,0,n-1)
fo(j,i+1,n-1)
{
int x1=max(x[i],x[j]),x2=min(x[i],x[j]);
int y1=max(y[i],y[j]),y2=min(y[i],y[j]);
s[j][i]=s[i][j]=(x1-x2)*(y1-y2);
if(x1==x2) s[i][j]=s[j][i]=y1-y2;
if(y1==y2) s[i][j]=s[j][i]=x1-x2;
if(x1==x2 && y1==y2) s[i][j]=s[j][i]=1;
fo(k,0,n-1)
if(x[k]<=x1 && x[k]>=x2 && y[k]<=y1 && y[k]>=y2)
f[i][j]|=(1<<k),f[j][i]|=(1<<k);
}
}
inline int dynamic()
{
int i,j,k;
dp[0]=0;
fo(i,0,(1<<n)-1)
{
fo(j,0,n-1)
{
fo(k,0,n-1)
dp[i|f[j][k]]=min(dp[i|f[j][k]],dp[i]+s[j][k]);
}
}
return dp[(1<<n)-1];
}
int main()
{
// freopen("rand.txt","r",stdin);
// freopen("orz.txt","w",stdout);
int i,j;
while(scanf("%d",&n)==1 && n)
{
init();
printf("%d\n",dynamic());
n=1;
}
return 0;
}
/*
5
3 0
2 4
3 1
2 3
1 4
*/