zoukankan      html  css  js  c++  java
  • poj 2836 Rectangular Covering

    Rectangular Covering
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2776   Accepted: 790

    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个顶点,现在需要用几个长方形取覆盖这些顶点,并且这些长方形中每一个都至少要覆盖住两个顶点。要用总面积尽量小的长方形覆盖住所有的顶点,至少要多少面积的长方形。
    思路:先把所有可能的长方形都找出来,并记录每个长方形可以覆盖那几个顶点。设dp[S]:顶点的覆盖情况为状态S时需要的最少的长方形面积。则转移方程为:dp[S]=min(dp[S],dp[k]+j_area);(S=k|points),其中points为某一个长方形j所能覆盖的顶点集,即状态S可以由状态k通过加上长方形j所能覆盖的顶点集转移而来,j_area为长方形j的面积。
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <iostream>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    #include<bitset>
    #include<set>
    #include<map>
    #include<cmath>
    using namespace std;
    #define N_MAX 16
    #define MOD 100000000
    #define INF 0x3f3f3f3f
    typedef long long ll;
    struct point {
        int x, y;
        point(int x=0,int y=0):x(x),y(y) {}
    }p[N_MAX];
    struct Rec {
        int area,points;//points代表当前的rectangle包含的顶点
        Rec(int area=0,int points=0):area(area),points(points) {}
    };
    int calc_area(const point& a,const point& b) {//计算矩形面积
        int s = max(abs(a.x - b.x),1)*max(abs(a.y-b.y),1);
        return s;
    }
    bool is_inarea(const point &a,const point& b,const point& c) {//点c是否在a,b构成的矩形内
        return  ((c.x - a.x)*(c.x - b.x) <= 0 && (c.y - a.y)*(c.y - b.y) <= 0);
    
    }
    int n;
    int dp[1 << N_MAX];//状态i下的最小面积
    vector<Rec> rec;
    int main() {
        while (scanf("%d",&n)&&n) {
            rec.clear();
            for (int i = 0; i < n;i++){
                scanf("%d%d",&p[i].x,&p[i].y);
            }
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n;j++) {//寻找所有的长方形,并且记录这些长方形包含了哪些顶点
                    Rec r=Rec(calc_area(p[i], p[j]), (1 << i) | (1 << j));
                    for (int k = 0; k < n;k++) {
                        if (k == i || k == j)continue;
                        if (is_inarea(p[i], p[j], p[k]))
                            r.points |= 1 << k;
                    }
                    rec.push_back(r);
                }
            }
            memset(dp, INF, sizeof(dp));
            int allstates = 1 << n;
            dp[0] = 0;
            for (int i = 0; i < rec.size();i++) {//每加入一个长方形
                for (int j = 0; j < allstates;j++) {
                    int newstate = j | rec[i].points;
                    if (dp[j] != INF&&newstate != j) {
                        dp[newstate] = min(dp[newstate], dp[j] + rec[i].area);
                    }
                }
            }
            printf("%d
    ",dp[allstates-1]);//全部顶点都加入的情况下最小面积
        }
        return 0;
    }
  • 相关阅读:
    操作正则表达式的公共类
    验证码类
    强制转化常用函数
    实现Base64编码与其它编码转换的类
    关于任务管理器踩过的坑,程序员必看!
    每次找Internet选项感到抓狂?一键打开!
    教你避过安装TensorFlow的两个坑
    HTML中id与name的通俗区别
    VS code代码对齐快捷键
    form和table的区别
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/8735459.html
Copyright © 2011-2022 走看看