Rectangular Covering POJ - 2836(状压dp)

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 xy (−1,000 ≤ xy ≤ 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

思路:一次性考虑包括两个点的面积必然是最小的,所以先预处理所有两点的状态,同时看有没有点能被包含。

最后dp一下就OK了。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=3e5+10;
const int MAXN=1e3+10;
using namespace std;
int n;
int dp[1<<16];
inline bool check(int x1,int y1,int x2,int y2,int x,int y)
{
    if(x1>x2) swap(x1,x2);
    if(y1>y2) swap(y1,y2);
    return x>=x1&&x<=x2&&y>=y1&&y<=y2;
}
inline int area(int x1,int y1,int x2,int y2)
{
    if(x1==x2) ++x2;
    if(y1==y2) ++y2;
    if(x1>x2) swap(x1,x2);
    if(y1>y2) swap(y1,y2);
    return (x2-x1)*(y2-y1);
}
struct Point
{
    int sta;
    int area;
}p[maxn];
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    while(scanf("%d",&n)!=EOF&&n)
    {
        int x[30],y[30];
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
        }
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                p[cnt].area=area(x[i],y[i],x[j],y[j]);
                p[cnt].sta=0;
                p[cnt].sta|=(1<<i);
                p[cnt].sta|=(1<<j);
                for(int k=0;k<n;k++)
                {
                    if(k==i||k==j) continue;
                    if(p[cnt].sta&(1<<k)) continue;
                    if(check(x[i],y[i],x[j],y[j],x[k],y[k]))
                    {
                        p[cnt].sta|=(1<<k);
                    }
                }
                cnt++;
            }
        }
        memset(dp,INF,sizeof(dp));
        dp[0]=0;
        int up=(1<<n)-1;
            for(int s=0;s<=up;s++)
            {
                    for(int j=0;j<cnt;j++)
                    {
                        dp[(s|(p[j].sta))]=min(dp[s|p[j].sta],dp[s]+p[j].area);
                    }
            }
        int ans=dp[up];
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81807758
今日推荐