poj 2836 Rectangular Covering

本文探讨了一种算法解决特定的矩形覆盖问题,即如何用最少面积的矩形覆盖平面上的点集,每个矩形至少覆盖两点,且矩形边平行于坐标轴。通过动态规划方法实现了最优解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
*/ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值