poj 2836 Rectangular Covering(状态压缩dp)
2015-08-29 02:38
507 查看
Descriptionn 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. Source PKU Local 2006, kicc 先预处理数据,将n个点两两组合形成n * (n-1) / 2个矩形,计算每个矩形的面积和内部点个数。 接着利用预处理数据来枚举,定义 dp[S] := 矩形集为S时的最省面积 先假设平面上没有矩形,那么dp[0]=0,接着一个一个地往平面上加矩形,递推关系是: dp[新矩形集合] = min(dp[新矩形集合], dp[旧矩形集合] + 新矩形的面积); #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define inf 1<<29 #define N 16 #define M 1<<N int x ,y ; int state ; int area ; int dp[M]; int main() { int n; while(scanf("%d",&n)==1){ if(n==0) break; memset(state,0,sizeof(state)); for(int i=0;i<n;i++){ scanf("%d%d",&x[i],&y[i]); } for(int i=0;i<n;i++){ for(int j=0;j<i;j++){ int lx=min(x[i],x[j]),rx=max(x[i],x[j]); int ly=min(y[i],y[j]),ry=max(y[i],y[j]); for(int k=0;k<n;k++){ if(lx<=x[k] && x[k]<=rx && ly<=y[k] && y[k]<=ry){ state[i][j]+=1<<k; } } int a=rx-lx?rx-lx:1; int b=ry-ly?ry-ly:1; area[i][j]=a*b; } } for(int i=0;i<(1<<n);i++) dp[i]=inf; dp[0]=0; for(int i=0;i<(1<<n);i++){ for(int j=0;j<n;j++){ for(int k=0;k<n;k++){ int T= i|state[j][k]; dp[T]=min(dp[T],dp[i]+area[j][k]); } } } printf("%d\n",dp[(1<<n)-1]); } return 0; } View Code |
相关文章推荐
- cordova+angularjs
- AngularJS从构建项目开始
- AngularJS入门教程08:更多模板
- ionic+AngularJs项目中实现三级联动效果,筛选省份、城市、区县
- POJ2836--Rectangular Covering
- Angular Js文件上传之form-data
- [Angularjs]ng-select和ng-options
- Angular Js文件上传之form-data
- angularjs ui-route
- angular form-data文件上传
- angularjs指令系统
- Angular新手容易碰到的坑
- AngularJS 应用身份认证的技巧
- angularjs 点滴汇总
- ngGrid checkbox应用
- ionic中点击图片看大图的实现
- JavaScript调用AngularJS的函数/$scope/变量
- angularjs 去往其他页面,目标页面controller中的方法无法自动加载
- angularjs 切换页面传参数
- angularjs 页面返回 参数传递