poj 2836 Rectangular Covering(状压DP)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了poj 2836 Rectangular Covering(状压DP),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2498字,纯文字阅读大概需要4分钟。
内容图文
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. 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.
给n个点,用矩形覆盖,让所有的点都被覆盖,求覆盖的矩形面积最小。
先枚举n个点组成的n*(n-1)个矩形,算出它的面积,并记录矩形内的点,最后dp求将所有的点覆盖的最小面积。
代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int dp[1<<15],s[400],area[400]; struct node { int x; int y; }p[20]; int abss(int x) { if(x<0) return -x; else return x; } int main() { int n,cur; while(~scanf("%d",&n)&&n) { cur=0; for(int i=0;i<n;i++) { scanf("%d%d",&p[i].x,&p[i].y); } memset(s,0,sizeof(s)); for(int i=0;i<n;i++)//枚举所有的矩形,并算出面积 { for(int j=i+1;j<n;j++) { s[cur]=(1<<i)|(1<<j); for(int k=0;k<n;k++ ) { if(k==i||k==j) continue; if((p[i].x-p[k].x)*(p[j].x-p[k].x)<=0&&(p[i].y-p[k].y)*(p[j].y-p[k].y)<=0)//判断k点在i,j组成的矩形内 { s[cur]|=(1<<k);//将k点放入i,j组成的矩形内 } } //计算面积 if(p[i].x==p[j].x) area[cur]=abss(p[i].y-p[j].y); else if(p[i].y==p[j].y) { area[cur]=abss(p[i].x-p[j].x); } else { area[cur]=abss(p[i].x-p[j].x)*abss(p[i].y-p[j].y); } cur++; } } // printf("jkds\n"); dp[0]=0; for(int i=1;i<(1<<n);i++ ) dp[i]=99999999; for(int i=0;i<(1<<n);i++) { for(int j=0;j<cur;j++) { dp[i|s[j]]=min(dp[i|s[j]],dp[i]+area[j]);//原来的选的点的集合为i,判断是否选第j个矩形, } } printf("%d\n",dp[(1<<n)-1]); } return 0; }
原文:http://blog.csdn.net/caduca/article/details/40474309
内容总结
以上是互联网集市为您收集整理的poj 2836 Rectangular Covering(状压DP)全部内容,希望文章能够帮你解决poj 2836 Rectangular Covering(状压DP)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。