Gym 101243 I Land Division[计算几何]

题意:顺时针顺序给定一个 N 个点的凸多边形,问是否能用一条线段将其分成一个M边形和一个 K 边形,如果能输出最短线段的长度,如果不能,输出1。其中 3N,M,K100

分析:通过观察我们可以发现,存在解的情况只有三种 :
1.N+2=M+K
2.N+3=M+K
3.N+4=M+K
如果不是上述三种情况直接输出 1

如果是第一种情况,说明线段两个端点都在多边形的顶点上,直接遍历所有顶点,我们可以通过一个点确定另一个端点。计算两点间距离取最小值即可.

如果是第二种情况,说明线段两个端点一个在顶点上,另一个在一条边上。同样的,我们还是遍历所有顶点,确定一个点可以从两个方向获得两条边,做两次点到线段距离取最小值。

如果是第三种情况,说明线段两个端点分别在两条边上,我们遍历每个顶点,获取两条边。两条边之间的最短距离通过4次点到线段最短距离得到。

以下是代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
typedef pair<int, int>pii;
const int INF = 0x3f3f3f3f;
const int MAXN = 3010;
const int MAXM = 100005;
const ll MOD = 998244353;
const int maxn = 205 + 100;
const double eps = 1e-8;
const double PI = acos(-1.0);
int sgn(double x)
{
    if (fabs(x) < eps)return 0;
    if (x < 0)return -1;
    else return 1;
}
struct Point
{
    double x, y;
    Point() {}
    Point(double _x, double _y)
    {
        x = _x; y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x, y - b.y);
    }
    //叉积
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    //点积
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
    //绕原点旋转角度B(弧度值),后x,y的变化
    void transXY(double B)
    {
        double tx = x, ty = y;
        x = tx*cos(B) - ty*sin(B);
        y = tx*sin(B) + ty*cos(B);
    }
}poo[105];
double dist(Point a, Point b)
{
    return sqrt((a - b)*(a - b));
}
struct Line
{
    Point s, e;
    Line() {}
    Line(Point _s, Point _e)
    {
        s = _s; e = _e;
    }
    //两直线相交求交点
    //第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交
    //只有第一个值为2时,交点才有意义
    pair<int, Point> operator &(const Line &b)const
    {
        Point res = s;
        if (sgn((s - e) ^ (b.s - b.e)) == 0)
        {
            if (sgn((s - b.e) ^ (b.s - b.e)) == 0)
                return make_pair(0, res);//重合
            else return make_pair(1, res);//平行
        }
        double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e));
        res.x += (e.x - s.x)*t;
        res.y += (e.y - s.y)*t;
        return make_pair(2, res);
    }
};

Point NearestPointToLineSeg(Point P, Line L)
{
    Point result;
    double t = ((P - L.s)*(L.e - L.s)) / ((L.e - L.s)*(L.e - L.s));
    if (t >= 0 && t <= 1)
    {
        result.x = L.s.x + (L.e.x - L.s.x)*t;
        result.y = L.s.y + (L.e.y - L.s.y)*t;
    }
    else
    {
        if (dist(P, L.s) < dist(P, L.e))
            result = L.s;
        else result = L.e;
    }
    return result;
}

int main() {

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < n; ++i) {
        scanf("%lf%lf", &poo[i].x, &poo[i].y);
    }
    double ans = 1e10;
    if (n + 2 == m + k) {
        for (int i = 0; i < n; ++i) {
            int now = (i + m - 1) % n;
            ans = min(ans, dist(poo[i], poo[now]));
        }
        printf("%.3f\n", ans);
    }
    else if (n + 3 == m + k) {
        for (int i = 0; i < n; ++i) {
            int now1 = (i + m - 1 + n) % n, now2 = (i + m - 2 + n) % n;
            Line L = Line(poo[now2], poo[now1]);
            Line L2 = Line(poo[i], poo[(i + 1) % n]);
            ans = min(ans, dist(poo[i], NearestPointToLineSeg(poo[i], L)));
            ans = min(ans, dist(poo[now1], NearestPointToLineSeg(poo[now1], L2)));
        }
        printf("%.3f\n", ans);
    }
    else if (n + 4 == m + k) {
        for (int i = 0; i < n; ++i) {
            int a = i, b = (i + 1) % n, c = (i + m - 1 + n) % n, d = (i + m - 2 + n) % n;

            ans = min(ans, dist(poo[a], NearestPointToLineSeg(poo[a], Line(poo[c], poo[d]))));
            ans = min(ans, dist(poo[b], NearestPointToLineSeg(poo[b], Line(poo[c], poo[d]))));
            ans = min(ans, dist(poo[c], NearestPointToLineSeg(poo[c], Line(poo[a], poo[b]))));
            ans = min(ans, dist(poo[d], NearestPointToLineSeg(poo[d], Line(poo[a], poo[b]))));
        }
        printf("%.3f\n", ans);
    }
    else printf("-1\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值