POJ 2836 Rectangular Covering (状压DP)

题意:平面上有 n (2 ≤ n ≤ 15) 个点,现用平行于坐标轴的矩形去覆盖所有点,每个矩形至少盖两个点,矩形面积不可为0,求这些矩形的最小面积。

析:先预处理所有的矩形,然后dp[s] 表示 状态 s 时,最少需要的面积是多少。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100000 + 10;
const int mod = 100000000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
int x[20], y[20];
struct Node{
  int area, cover;
  Node(int s,int c) : area(s), cover(c) { }
};
vector<Node> rec;

void calc(int i, int j, int &s, int &cover){
  int w = max(abs(x[i] - x[j]), 1);
  int l = max(abs(y[i] - y[j]), 1);
  s = w * l;
  cover = 0;
  int minx = min(x[i], x[j]);
  int maxx = max(x[j], x[i]);
  int miny = min(y[i], y[j]);
  int maxy = max(y[j], y[i]);
  for(int i = 0; i < n; ++i)
    if(x[i] >= minx && y[i] <= maxy && x[i] <= maxx && y[i] >= miny)  cover |= 1<<i;
}

int dp[1<<15];

int main(){
  while(scanf("%d", &n) == 1 && n){
    rec.clear();
    for(int i = 0; i < n; ++i)
      scanf("%d %d", x+i, y+i);
    for(int i = 1; i < n; ++i)
      for(int j = 0; j < i; ++j){
        int cover, s;
        calc(i, j, s, cover);
        rec.push_back(Node(s, cover));
      }
    memset(dp, INF, sizeof dp);
    dp[0] = 0;
    int all = 1 << n;
    for(int j = 0; j < rec.size(); ++j){
      Node &u = rec[j];
      for(int i = 0; i < all; ++i){
        if(dp[i] == INF)  continue;
        dp[i|u.cover] = min(dp[i|u.cover], dp[i] + u.area);
      }
    }
    printf("%d\n", dp[all-1]);
  }
  return 0;
}

  

时间: 2024-07-28 23:19:25

POJ 2836 Rectangular Covering (状压DP)的相关文章

poj 2836 Rectangular Covering(状压DP)

Rectangular Covering Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1716   Accepted: 468 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. Ever

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

POJ 2836 Rectangular Covering 题解 《挑战程序设计竞赛》

POJ 2836 Rectangular Covering铺地板:坐标平面上有n各点,用任意大小(非零)的地板砖覆盖它们,求最省的地板砖总面积.3.4熟练掌握动态规划状态压缩DP先预处理数据,将n个点两两组合形成n * (n-1) / 2个矩形,计算每个矩形的面积和内部点个数.接着利用预处理数据来枚举,定义dp[S] := 矩形集为S时的最省面积先假设平面上没有矩形,那么dp[0]=0,接着一个一个地往平面上加矩形,递推关系是:dp[新矩形集合] = min(dp[新矩形集合], dp[旧矩形集

POJ 1185 炮兵阵地 状压dp

http://poj.org/problem?id=1185 经典题目不必多说,直接贴代码. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 int n, m, cnt, size; 7 int a[110], st[70], ct[70]; 8 char str[15]; 9 int f[110][70][70]; 10 void init(

POJ 1185 炮兵阵地 状压DP+离散化优化

一开始能想到的状态就只有位压两行和当前行的行号,这样无论是空间和时间都是无法接受的. 但是因为炮兵的攻击范围比较大,而且又有地形限制,每一行的状态其实不多了,打表看了一下不超过80种,离散化一下就可以随意DP了. 据说题目也可以抽象成二分图最大匹配来搞?感觉复杂度有点高 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #i

POJ 3254 Corn Fields 状压DP

链接:http://poj.org/problem?id=3254 题意:一块M*N的田地,每小块地大小是1*1,可以种植物的标记为1,不可以种植物的标记为0,并且相邻的两块地不可以同时种植物.问题是有多少种不同的种植方案(所有地都不种也是一种种植方案) 思路:这是第一道状压DP题,从第一行更新到最后一行,每一行用一个N位的二进制数来表示该行的状态1表示该位置种了植物,0表示该位置没种植物.因为每块地只对相邻的土地能否种植有所影响,所以每一行的状态可以用前一行的状态递推得到. 资料:http:/

POJ 1185 炮兵布阵 状压DP

链接:http://poj.org/problem?id=1185 题意:一个地图上有两种地形,H和P,P上可以放一个炮,攻击范围是上下左右各两格,问的是最多可以再地图上放多少个炮.行N <= 100,列M <= 10. 思路:因为上下左右各两格内不能放置炮,所以每一行的状态数从2^10减少到60种.状态转移方程为:dp[i][j][k]=max(dp[i-1][k][l]+bb[j]).dp[i][j][k]表示在第i行状态为j,在第i-1行状态为k的前i行一共放置的炮塔数.bb[j]表示状

POJ 1185 炮兵阵地 (状压dp 经典中的经典)

炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21381   Accepted: 8290 Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用"P"表示),如下图.在每一格平原地形上最多可以布置一支炮兵部队(山地上不能够部署炮兵部队):一支炮兵部队在地图上的攻击

POJ 1684 Corn Fields(状压dp)

描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be