题目链接:http://poj.org/problem?id=1271
Time Limit: 1000MS Memory Limit: 10000K
Description
Little Tomy likes to cover his bread with some milk. He does this by putting it in the cup so that one of its sides (called bottom side) touches the bottom of the cup, just as the picture below:
Since the milk in the cup is limited, only part of the bread is covered with milk(as shown in the pictures). That is, only the area between the surface of the milk and the bottom side of the bread is covered. Note that the distance between these two lines is always h - the depth of the milk, which is also known to him.
Tomy wants to cover this bread with largest possible area of milk in this way, but he doesn't want to do more than k actions. Help him, will you?
(You may assume that the cup is wide enough, wider than any side of the bread, so it's possible to cover any side completely)
Input
The input will contain no more than 10 test cases. Each test case begins with a line containing three integers n,k and h (3<=n<=20, 0<=k<=8, 0<=h<=10). A piece of bread is guaranteed to be a convex polygon of n vertices. In the following n lines, each line contains two integers xi and yi(0<=xi,yi<=1000), representing the Cartesianism coordinate of the ith vertex. The vertices are anti-clockwise numbered. The test case containing n=0, k=0, h=0 will terminate the input, you should not give an answer to this case.
Output
Output the area of the largest possible area of bread covered with milk with two decimal places. Output one line for each test case.
Sample Input
4 2 1
1 0
3 0
5 2
0 4
0 0 0
Sample Output
7.46
Problem solving report:
Description: 给一个凸多边形的面包,和一瓶深度为h的牛奶,要沾牛奶k次,求可以沾到牛奶的面包的面积。
Problem solving: 半平面相交.保存各个边被牛奶沾后的位置,即将直线向内推进h,用DFS找出要沾的边,对于每一种情况进行一次半平面交,然后计算剩下的面积,每次选取最小的面积,最后用总面积减去得到的面积就是答案了。
Accepted Code:
//#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 25;
const double eps = 10-8;
const double inf = 0x3f3f3f3f3f;
int n, k, vis[MAXN];
double min_;
typedef struct point {
double x, y;
}vect;
struct line {
vect p;
point u, v;
}Sl[MAXN], Sl_[MAXN];
vect operator - (const point a, const point b) {
vect p;
p.x = a.x - b.x;
p.y = a.y - b.y;
return p;
}
int pnz(double x) {
return x < -eps ? -1 : x > eps ? 1 : 0;
}
double Cross(const vect a, const vect b) {
return a.x * b.y - a.y * b.x;
}
double dist(const point a, const point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
bool Onleft(const point p, const line l) {
return pnz(Cross(l.p, p - l.u)) > 0;
}
point Inter(const line a, const line b) {
point p;
double t = Cross(b.p, a.u - b.u) / Cross(a.p, b.p);
p.x = a.u.x + t * a.p.x;
p.y = a.u.y + t * a.p.y;
return p;
}
void Mobile(line l[], int n, double r) {
double dis, dx, dy;
for (int i = 0; i < n; i++) {
dis = dist(l[i].u, l[i].v);
dx = (l[i].v.y - l[i].u.y) / dis * r;
dy = (l[i].v.x - l[i].u.x) / dis * r;
l[i].u.x -= dx, l[i].u.y += dy;
l[i].v.x -= dx, l[i].v.y += dy;
}
}
double Area(const point p[], int n) {
double area = 0;
for (int i = 1; i < n - 1; i++)
area += Cross(p[i] - p[0], p[i + 1] - p[0]);
return area / 2;
}
double Halfplane(line Sline[], int n) {
point Spt[MAXN];
int Q[MAXN] = {0};
int l = 0, r = 0;
for (int i = 1; i < n; i++) {
while (l < r && !Onleft(Spt[r - 1], Sline[i]))
r--;
while (l < r && !Onleft(Spt[l], Sline[i]))
l++;
Q[++r] = i;
if (!pnz(Cross(Sline[i].p, Sline[Q[r - 1]].p))) {
r--;
if (Onleft(Sline[i].u, Sline[Q[r]]))
Q[r] = i;
}
else Spt[r - 1] = Inter(Sline[i], Sline[Q[r - 1]]);
}
while (l < r && !Onleft(Spt[r - 1], Sline[Q[l]]))
r--;
while (l < r && !Onleft(Spt[l], Sline[Q[r]]))
l++;
if (r - l < 2)
return 0;
Spt[r] = Inter(Sline[Q[l]], Sline[Q[r]]);
return Area(Spt + l, r - l + 1);
}
double slove() {
int cnt = 0;
line Sline[MAXN];
for (int i = 0; i < n; i++)
if (vis[i])
Sline[cnt++] = Sl_[i];
else Sline[cnt++] = Sl[i];
return Halfplane(Sline, cnt);
}
void DFS(int l, int s) {
if (l >= k) {
min_ = min(min_, slove());
return ;
}
if (s >= n)
return ;
for (int i = 0; i < 2; i++) {
vis[s] = i;
DFS(l + i, s + 1);
}
vis[s] = 0;
}
void Copy(line l_[], line l[], int n) {
for (int i = 0; i < n; i++)
l_[i] = l[i];
}
int main() {
double r;
point p[MAXN];
while (scanf("%d%d%lf", &n, &k, &r), n + k + r) {
min_ = inf;
for (int i = 0; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
if (!k || !r) {
printf("0.00\n");
continue;
}
p[n] = p[0];
for (int i = 0; i < n; i++) {
Sl[i].u = p[i];
Sl[i].v = p[i + 1];
Sl[i].p = p[i + 1] - p[i];
}
Copy(Sl_, Sl, n);
Mobile(Sl_, n, r);
k = min(k, n);
DFS(0, 0);
printf("%.2f\n", Area(p, n) - min_);
memset(vis, 0, sizeof(vis));
}
return 0;
}