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=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; }