POJ 2836 状压DP

Rectangular Covering
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2727   Accepted: 769

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 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

Hint

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

Source

题意:坐标平面上有n各点,用任意大小(非零)的矩形覆盖它们,每个矩形至少覆盖亮点,求最省的矩形的总面积。

解析:先预处理,将n个点两个两个组合构成多个矩形,然后将在矩形内部的点更新进矩形中。

然后就是dp的过程了:dp[0] = 0,初始时集合无点,面积为0,dp[nowS] = min ( dp[nowS], dp[s] + area),当前的点集的矩阵取,还是不取。

代码:

 1 //#include "bits/stdc++.h"
 2 #include "cstdio"
 3 #include "map"
 4 #include "set"
 5 #include "cmath"
 6 #include "queue"
 7 #include "vector"
 8 #include "string"
 9 #include "cstring"
10 #include "time.h"
11 #include "iostream"
12 #include "stdlib.h"
13 #include "algorithm"
14 #define db double
15 #define ll long long
16 //#define vec vector<ll>
17 #define Mt  vector<vec>
18 #define ci(x) scanf("%d",&x)
19 #define cd(x) scanf("%lf",&x)
20 #define cl(x) scanf("%lld",&x)
21 #define pi(x) printf("%d
",x)
22 #define pd(x) printf("%f
",x)
23 #define pl(x) printf("%lld
",x)
24 #define inf 0x3f3f3f3f
25 #define rep(i, x, y) for(int i=x;i<=y;i++)
26 const int N   = 1e5 + 5;
27 const int mod = 1e9 + 7;
28 const int MOD = mod - 1;
29 const db  eps = 1e-10;
30 const db  PI  = acos(-1.0);
31 
32 using namespace std;
33 int n;
34 int x[20],y[20];
35 struct rec
36 {
37     int are,cov;
38     rec(){};
39     rec(int _are,int _cov) {are=_are,cov=_cov;}
40     void addP(int x)       {cov|=(1<<x);}//加点
41 };
42 int area(int i,int j)
43 {
44     return max(abs(x[i]-x[j]),1)*max(abs(y[i]-y[j]),1);
45 }
46 bool cal(int i,int j,int k){//判断第三点是否在前两点确定的矩形上
47     return (x[i]-x[k])*(x[j]-x[k])<=0&&(y[i]-y[k])*(y[j]-y[k])<=0;
48 }
49 vector<rec> e;
50 int f[N];
51 int main()
52 {
53     while(scanf("%d",&n)==1&&n!=0)
54     {
55         e.clear();
56         for(int i=0;i<n;i++)   ci(x[i]),ci(y[i]);
57         for(int i=0;i<n;i++){
58             for(int j=i+1;j<n;j++){
59                 rec tmp(area(i,j),(1<<i)|(1<<j));
60                 for(int k=0;k<n;k++) if(cal(i,j,k)) tmp.addP(k);//合并点集
61                 e.push_back(tmp);
62             }
63         }
64         memset(f,inf, sizeof(f));
65         f[0]=0;
66         for(int i=0;i<e.size();i++){
67             for(int j=0;j<(1<<n);j++){//从0开始更新,每次加入点集
68                 int S=j|e[i].cov;
69                 if(f[j]!=inf) f[S]=min(f[S],f[j]+e[i].are);
70             }
71         }
72         pi(f[(1<<n)-1]);
73     }
74     return 0;
75 }
原文地址:https://www.cnblogs.com/mj-liylho/p/8392861.html