POJ 2836 Rectangular Covering

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.


解题思路:状压DP的题目,dp[i]表示当前顶点集合所有顶点被覆盖时所需矩形面积的最小值,状态转移方程是枚举n个顶点中的每个顶点对,以这两个顶点作为矩形对角点增加一个矩形后所有被覆盖的顶点集合,用dp[i]+该矩形的面积去更新。因此需要预处理出顶i和顶点j构成的矩形能够覆盖的所有点的集合。代码写的比较乱,很多没有必要写,也懒得改了。

#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <numeric>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = 2010;
const int inf  = 0x3f3f3f3f;
int dp[(1<<15)+10];

struct Point {
    int x, y;
    Point() { }
    Point(int t_x, int t_y) : x(t_x), y(t_y) { }
    bool operator < (const Point &p) const {
        if(x == p.x) {
            return y < p.y;
        }
        return x < p.x;
    }
}point[20];

int st[20][20][2];
int area[20][20][2];
int p[20], np[20];

int main() {

    //freopen("aa.in", "r", stdin);

    int n;
    while(scanf("%d", &n) && n) {
        for(int i = 0; i < n; ++i) {
            scanf("%d %d", &point[i].x, &point[i].y);
        }
        sort(point, point + n);
        memset(st, 0, sizeof(st));
        memset(area, inf, sizeof(area));
        for(int i = 0; i < n; ++i) {
            for(int j = i + 1; j < n; ++j) {
                int x1 = min(point[i].x, point[j].x);
                int x2 = max(point[i].x, point[j].x);
                int y1 = min(point[i].y, point[j].y);
                int y2 = max(point[i].y, point[j].y);
                if(x1 == x2) {
                    area[i][j][0] = area[i][j][1] = y2 - y1;
                    st[i][j][0] |= (1<<i);
                    st[i][j][0] |= (1<<j);
                    st[i][j][1] |= (1<<i);
                    st[i][j][1] |= (1<<j);
                    for(int k = i + 1; k < j; ++k) {
                        if(point[k].x >= x1 - 1 && point[k].x <= x2 && point[k].y >= y1 && point[k].y <= y2) {
                            st[i][j][0] |= (1<<k);
                        }
                        if(point[k].x >= x1 && point[k].x <= x2 + 1 && point[k].y >= y1 && point[k].y <= y2) {
                            st[i][j][1] |= (1<<k);
                        }
                    }
                } else if(y1 == y2) {
                    area[i][j][0] = area[i][j][1] = x2 - x1;
                    st[i][j][0] |= (1<<i);
                    st[i][j][0] |= (1<<j);
                    st[i][j][1] |= (1<<i);
                    st[i][j][1] |= (1<<j);
                    for(int k = i + 1; k < j; ++k) {
                        if(point[k].x >= x1 && point[k].x <= x2 && point[k].y >= y1 - 1 && point[k].y <= y2) {
                            st[i][j][0] |= (1<<k);
                        }
                        if(point[k].x >= x1 && point[k].x <= x2 && point[k].y >= y1 && point[k].y <= y2 + 1) {
                            st[i][j][1] |= (1<<k);
                        }
                    }
                } else {
                    area[i][j][0] = (y2-y1)*(x2-x1);
                    st[i][j][0] |= (1<<i);
                    st[i][j][0] |= (1<<j);
                    for(int k = i + 1; k < j; ++k) {
                        if(point[k].x >= x1 && point[k].x <= x2 && point[k].y >= y1 && point[k].y <= y2) {
                            st[i][j][0] |= (1<<k);
                        }
                    }
                }
            }
        }
        memset(dp, inf, sizeof(dp));
        dp[0] = 0;
        for(int i = 0; i < (1<<n); ++i) {
            if(dp[i] == inf) continue;
            for(int j = 0; j < n; ++j) {
                for(int k = j + 1; k < n; ++k) {
                    int x1 = min(point[j].x, point[k].x);
                    int y1 = min(point[j].y, point[k].y);
                    int x2 = max(point[j].x, point[k].x);
                    int y2 = max(point[j].y, point[k].y);
                    if(x1 == x2 || y1 == y2) {
                        dp[i|st[j][k][0]] = min(dp[i|st[j][k][0]], dp[i] + area[j][k][0]);
                        dp[i|st[j][k][1]] = min(dp[i|st[j][k][1]], dp[i] + area[j][k][1]);
                    } else {
                        dp[i|st[j][k][0]] = min(dp[i|st[j][k][0]], dp[i] + area[j][k][0]);
                    }
                }
            }
        }
        printf("%d\n", dp[(1<<n)-1]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值