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
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.
因为要面积最小,所以每个矩形的一条对角线的两个顶点肯定是这些点,所以可以枚举出以i,j为顶点的矩形可以覆盖的集合,以及它的面积。
此题不可以出现面积为0的矩形,所以当出现x[i]==x[j]或者y[i]==y[j]时,让它为0的这边的边长变成1。
预处理,枚举任意两点,求它的面积,并看这两点之间还有多少点被覆盖,将被覆盖的所有点存入状态。
然后就可以DP了
代码如下
/*AC*/
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf = 1e9;
int n, m;
int x[15], y[15];
int dp[1<<15];
int state[15*14/2], squ[15*14/2];
void Init(){ //预处理所有只包含对角线上两个点的状态及其面积
m = 0;
for(int i=0; i<n-1; i++){
for(int j=i+1; j<n; j++){
state[m] = 0;
for(int k=0; k<n; k++){
if((x[i]-x[k])*(x[k]-x[j])>=0 && (y[i]-y[k])*(y[k]-y[j])>=0){ //用乘积符号判断点在矩形内,避免了比较对角线上两个点的大小
state[m] |= (1<<k);
}
}
if(x[i] == x[j]) squ[m] = abs(y[i]-y[j]); //对角线的两个端点共线的情况
else if(y[i] == y[j]) squ[m] = abs(x[i]-x[j]);
else squ[m] = abs(x[i]-x[j])*abs(y[i]-y[j]);
m++;
}
}
}
int main(){
while(scanf("%d", &n) && n){
for(int i=0; i<n; i++)
scanf("%d %d", &x[i], &y[i]);
Init();
dp[0] = 0;
for(int S=1; S<(1<<n); S++)
dp[S] = inf;
for(int S=0; S<(1<<n); S++){
for(int i=0; i<m; i++){
int T = S|state[i];
if(T == S) continue; //如果state[i]是S的子集
dp[T] = min(dp[T], dp[S]+squ[i]);
}
}
printf("%d\n", dp[(1<<n)-1]);
}
return 0;
}