zoukankan      html  css  js  c++  java
  • poj 2836 Rectangular Covering(状态压缩dp)

    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

    The total area is calculated by adding up the areas of rectangles used.

    Source

     

    先预处理数据,将n个点两两组合形成n * (n-1) / 2个矩形,计算每个矩形的面积和内部点个数。

    接着利用预处理数据来枚举,定义

    dp[S] := 矩形集为S时的最省面积

    先假设平面上没有矩形,那么dp[0]=0,接着一个一个地往平面上加矩形,递推关系是:

    dp[新矩形集合] = min(dp[新矩形集合], dp[旧矩形集合] + 新矩形的面积);

     
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 #define inf 1<<29
     6 #define N 16
     7 #define M 1<<N
     8 int x[N],y[N];
     9 int state[N][N];
    10 int area[N][N];
    11 int dp[M];
    12 int main()
    13 {
    14     int n;
    15     while(scanf("%d",&n)==1){
    16         if(n==0)
    17            break;
    18         memset(state,0,sizeof(state));
    19         for(int i=0;i<n;i++){
    20             scanf("%d%d",&x[i],&y[i]);
    21         }
    22         for(int i=0;i<n;i++){
    23             for(int j=0;j<i;j++){
    24                 int lx=min(x[i],x[j]),rx=max(x[i],x[j]);
    25                 int ly=min(y[i],y[j]),ry=max(y[i],y[j]);
    26                 for(int k=0;k<n;k++){
    27                     if(lx<=x[k] && x[k]<=rx && ly<=y[k] && y[k]<=ry){
    28                         state[i][j]+=1<<k;
    29                     }
    30                 }
    31                 int a=rx-lx?rx-lx:1;
    32                 int b=ry-ly?ry-ly:1;
    33                 area[i][j]=a*b;
    34             }
    35         }
    36         
    37         for(int i=0;i<(1<<n);i++)
    38           dp[i]=inf;
    39         dp[0]=0;
    40         for(int i=0;i<(1<<n);i++){
    41             for(int j=0;j<n;j++){
    42                 for(int k=0;k<n;k++){
    43                     int T= i|state[j][k];
    44                     dp[T]=min(dp[T],dp[i]+area[j][k]);
    45                 }
    46             }
    47         }
    48         printf("%d
    ",dp[(1<<n)-1]);
    49     }
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    已有模板与tp框架结合
    模板文件引入css样式文件
    通过vertical-align属性实现“竖向居中”显示
    解决PHP服务端返回json字符串有特殊字符的问题
    PHP数组排序函数:sort、asort和ksort的不同
    PHP常用开发函数解析之数组篇
    PHP将数组存入数据库中的四种方式
    PHP foreach的两种用法 as $key => $value
    sharepoint database 操作
    Enabling Remote Errors in SSRS
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4768286.html
Copyright © 2011-2022 走看看