Nice Milk POJ - 1271 UVA 10117 半平面相交

题目链接: POJ , UVa


    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

Source

OIBH Online Programming Contest(OOPC)#1

题意:

求能沾到牛奶的最大的面积.

思路:

半平面相交. 将直线向内推进 h,
dfs 枚举每一次选取边的所有情况, 对于每一种情况进行一次半平面交, 然后计算剩下的面积, 每次选取最小的面积, 最后用总面积减去得到的面积就是答案了,

不过这题 poj 很容易超时, 我最开始就是在 uva 上 380ms 过了, 然后在 poj 上各种花式T, T 到怀疑人生, 后来改了一下 dfs 函数,只有减少了 计算次数, 780ms 跑过.

代码;

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

#define sqr(x) (x)*(x)
const double eps = 1e-8;
typedef long long LL;

typedef struct point{
    double x, y;
    point(){}
    point (double x, double y)
    :x(x), y(y){}
    point operator + (point q){
        return point(x+q.x, y+q.y);
    }

    point operator - (point q){
        return point(x-q.x, y-q.y);
    }

    point operator = (point q){
        x = q.x;
        y = q.y;
        return *this;
    }
}point;

int dcmp(double x){
    if(fabs(x) < eps) return 0;
    return x > 0 ? 1 : -1;
}

double dot(point p, point q){
    return p.x * q.x + p.y * q.y;
}

double det(point p, point q){
    return p.x * q.y - p.y * q.x;
}

double cross(point p, point q1, point q2){
    return det(q1-p,q2-p);
}

double dis(point p, point q){
    return sqrt(sqr(p.x-q.x) + sqr(p.y-q.y));
}


const double INF = 1000000000.0;
point p[30], q[30],qq[30], tmp[30];
double a, b ,c;
int newn, n, h, k;
bool vis[30];
double ans;

void Getabc(point p, point q){
    a = q.y - p.y;
    b = p.x - q.x;
    c = -q.y * p.x + p.y * q.x;;
}

point inter_point(point p, point q){//计算交点坐标
    double u = fabs(a * p.x + b * p.y + c);
    double v = fabs(a * q.x + b * q.y + c);
    point t;
    t.x = (u * q.x + v * p.x) / (u + v);
    t.y = (u * q.y + v * p.y) / (u + v);
    return t;
}

double area(point t[], int cnt){
    double ans = 0;
    for(int i = 2; i < cnt; ++i)
        ans += cross(t[1],t[i],t[i+1]);
    if(ans < 0) ans = -ans;
    return ans/2.0;
}

void cut(){
    int cnt = 0;
    for(int i = 1; i <= newn; ++i){
        if(a * q[i].x + b * q[i].y + c >= 0)
            tmp[++cnt] = q[i];
        else {
            if(a * q[i-1].x + b * q[i-1].y + c > 0)
                tmp[++cnt] = inter_point(q[i], q[i-1]);
            if(a * q[i+1].x + b * q[i+1].y + c > 0)
                tmp[++cnt] = inter_point(q[i], q[i+1]);
        }
    }for(int i = 1; i <= cnt; ++i)
        q[i] = tmp[i];
    newn = cnt;
    q[0] = q[newn];
    q[newn+1] = q[1];
}

void solve( ){
    newn = 0;
    for(int i = 1; i <= n; ++i){
        q[i] = p[i];
        if(vis[i]){//由于懒得再开一个结构体, 所以直接用连续两个点代替了直线
            qq[++newn] = p[i];
            qq[++newn] = p[i+1];

    }
    }q[0] = q[n];
    q[n+1] = q[1];
    int cnt = newn;
    newn = n;
    for(int i = 1; i < cnt; i += 2){
        point t, ta, tb;//向内推进 h,
        //与直线垂直的方向向量,
        t.x = qq[i+1].y - qq[i].y;
        t.y = qq[i].x - qq[i+1].x;
        double kk = h / sqrt(sqr(t.x) + sqr(t.y));
        //需要向内推进的坐标长度
        t.x *= kk;
        t.y *= kk;
        ta.x = qq[i].x + t.x;
        ta.y = qq[i].y + t.y;
        tb.x = qq[i+1].x + t.x;
        tb.y = qq[i+1].y + t.y;
        Getabc(ta, tb);
        cut();
    }
}

void dfs(int i, int num){
    if(ans == 0.0) return ;
    if(num == k || i == n){//就是这里, 不要每次都计算,
        solve();
        ans = min(ans, area(q, newn));
    }
    if(num >= k || i >= n) return;
    dfs(i+1, num);
    vis[i+1] = true;
    dfs(i+1, num+1);
    vis[i+1] = false;
}

int main(){
    while(scanf("%d %d %d", &n, &k, &h) != EOF){
        if(n == 0 && k == 0 && h == 0) break;
        memset(vis, false, sizeof(vis));
        for(int i = n; i >= 1; --i) //题目是逆时针, 所以我倒着输入
            scanf("%lf %lf", &p[i].x, &p[i].y);
        p[n+1] = p[1];
        ans = area(p,n); //得到总面积.
        double ans2 = ans;
        dfs(0, 0);
        printf("%.2f\n",ans2-ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值