poj3384-Feng Shui 求多边形上最远的两点(旋转卡壳算法)

Feng Shui
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5771 Accepted: 1743 Special Judge

Description

Feng shui is the ancient Chinese practice of placement and arrangement of space to achieve harmony with the environment. George has recently got interested in it, and now wants to apply it to his home and bring harmony to it.

There is a practice which says that bare floor is bad for living area since spiritual energy drains through it, so George purchased two similar round-shaped carpets (feng shui says that straight lines and sharp corners must be avoided). Unfortunately, he is unable to cover the floor entirely since the room has shape of a convex polygon. But he still wants to minimize the uncovered area by selecting the best placing for his carpets, and asks you to help.

You need to place two carpets in the room so that the total area covered by both carpets is maximal possible. The carpets may overlap, but they may not be cut or folded (including cutting or folding along the floor border) — feng shui tells to avoid straight lines.

Input

The first line of the input file contains two integer numbers n and r — the number of corners in George’s room (3 ≤ n ≤ 100) and the radius of the carpets (1 ≤ r ≤ 1000, both carpets have the same radius). The following n lines contain two integers xi and yi each — coordinates of the i-th corner (−1000 ≤ xiyi ≤ 1000). Coordinates of all corners are different, and adjacent walls of the room are not collinear. The corners are listed in clockwise order.

Output

Write four numbers x1y1x2y2 to the output file, where (x1y1) and (x2y2) denote the spots where carpet centers should be placed. Coordinates must be precise up to 4 digits after the decimal point.

If there are multiple optimal placements available, return any of them. The input data guarantees that at least one solution exists.

Sample Input

 
 
#15 2 -2 0 -5 3 0 8 7 3 5 0
#24 3 0 0 0 8 10 8 10 0

Sample Output

 
 
#1-2 3 3 2.5
#23 5 7 3

Hint


题目的意思是说在多边形内安排两个圆,使得两个圆覆盖的区域尽可能大(重合的部分只算一次),求两个圆的圆心坐标。

这题的核心是找凸多边形上距离最远的两顶点,所以可以运用旋转卡壳算法。

ac代码如下:

#include<stdio.h>
#include<string.h>
#include<math.h>
#define MAXD 210
#define zero 1e-8
#define INF 2000
struct point
{
    double x, y;
}p[MAXD], wa[MAXD], wb[MAXD], *a, *b;
int N, R, na, nb;
double det(double x1, double y1, double x2, double y2)
{
    return x1 * y2 - x2 * y1;
}
int dcmp(double x)
{
    return fabs(x) < zero ? 0 : (x < 0 ? -1 : 1);
}
double sqr(double x)
{
    return x * x;
}
void init()
{
    int i, j, k;
    for(i = 0; i < N; i ++)
        scanf("%lf%lf", &p[i].x, &p[i].y);
    p[N] = p[0];
}
void add(double x, double y)
{
    b[nb].x = x, b[nb].y = y;
    ++ nb;
}
void cut(int k)
{
    int i, j;
    double x, y, t1, t2, dx, dy;
    t1 = sqrt(sqr(p[k + 1].x - p[k].x) + sqr(p[k + 1].y - p[k].y));
    dx = (p[k].y - p[k + 1].y) / t1 * R;
    dy = (p[k + 1].x - p[k].x) / t1 * R;
    point *t;
    nb = 0;
    for(i = 0; i < na; i ++)
    {
        t1 = det(p[k + 1].x - p[k].x, p[k + 1].y - p[k].y, a[i].x + dx - p[k].x, a[i].y + dy - p[k].y);
        t2 = det(p[k + 1].x - p[k].x, p[k + 1].y - p[k].y, a[i + 1].x + dx - p[k].x, a[i + 1].y + dy - p[k].y);
        if(dcmp(t1) <= 0)
            add(a[i].x, a[i].y);
        if(dcmp(t1) * dcmp(t2) < 0)
        {
            x = (fabs(t2) * a[i].x + fabs(t1) * a[i + 1].x) / (fabs(t1) + fabs(t2));
            y = (fabs(t2) * a[i].y + fabs(t1) * a[i + 1].y) / (fabs(t1) + fabs(t2));
            add(x, y);
        }
    }
    t = a, a = b, b = t;
    na = nb;
    a[na] = a[0];
}
void solve()
{
    int i, j, k;
    double t, max, x1, y1, x2, y2;
    a = wa, b = wb;
    na = 4;
    a[0].x = -INF, a[0].y = -INF, a[1].x = -INF, a[1].y = INF, a[2].x = INF, a[2].y = INF, a[3].x = INF, a[3].y = -INF;
    a[na] = a[0];
    for(i = 0; i < N; i ++)
        cut(i);
    max = -1.0;
    for(i = 0; i < na; i ++)
        for(j = i; j < na; j ++)
        {
            t = sqrt(sqr(a[i].x - a[j].x) + sqr(a[i].y - a[j].y));
            if(dcmp(t - max) > 0)
            {
                x1 = a[i].x, y1 = a[i].y;
                x2 = a[j].x, y2 = a[j].y;
                max = t;
            }
        }
    printf("%.5lf %.5lf %.5lf %.5lf\n", x1, y1, x2, y2);
}
int main()
{
    while(scanf("%d%d", &N, &R) == 2)
    {
        init();
        solve();
    }
    return 0;
}


题目网址 点击打开链接http://poj.org/problem?id=3384


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值