poj 2836 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 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
#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=,int y=):x(x),y(y) {}
}p[N_MAX];
struct Rec {
int area,points;//points代表当前的rectangle包含的顶点
Rec(int area=,int points=):area(area),points(points) {}
};
int calc_area(const point& a,const point& b) {//计算矩形面积
int s = max(abs(a.x - b.x),)*max(abs(a.y-b.y),);
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) <= && (c.y - a.y)*(c.y - b.y) <= ); }
int n;
int dp[ << N_MAX];//状态i下的最小面积
vector<Rec> rec;
int main() {
while (scanf("%d",&n)&&n) {
rec.clear();
for (int i = ; i < n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
}
for (int i = ; i < n; i++) {
for (int j = i + ; j < n;j++) {//寻找所有的长方形,并且记录这些长方形包含了哪些顶点
Rec r=Rec(calc_area(p[i], p[j]), ( << i) | ( << j));
for (int k = ; k < n;k++) {
if (k == i || k == j)continue;
if (is_inarea(p[i], p[j], p[k]))
r.points |= << k;
}
rec.push_back(r);
}
}
memset(dp, INF, sizeof(dp));
int allstates = << n;
dp[] = ;
for (int i = ; i < rec.size();i++) {//每加入一个长方形
for (int j = ; 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\n",dp[allstates-]);//全部顶点都加入的情况下最小面积
}
return ;
}
最新文章
- [MongoDB]入门操作
- SDWebImage ReadMe.md文档简单说明
- uC/OS-II源码分析
- C++数组(指针)作为函数参数
- WebService的简单运用添加删除
- python 函数运算先于单目运算
- DataReport使用手记
- php cookie的问题
- kernel: INFO: task sadc:14833 blocked for more than 120 seconds.
- 【逆向工具】IDA使用5-( string、图形化与视图的切换、图形化显示反汇编地址、自动注释、标签使用)
- SSL/TLS中间人攻击
- BBR,附CentOS 6/7配置过程
- 8 个基于 Lucene 的开源搜索引擎推荐
- 1347: Last Digit (周期函数)
- 中文乱码—Servlet—SpringMVC
- python 去除不可见的控制字符
- iOS&#39;s GCD Note
- php 导出csv表格文件
- 第二章 shell的语法
- Java编程介绍