D - Grandpa‘s Estate(稳定凸包)

Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa's belongings. The most valuable one was a piece of convex polygon shaped farm in the grandpa's birth village. The farm was originally separated from the neighboring farms by a thick rope hooked to some spikes (big nails) placed on the boundary of the polygon. But, when Kamran went to visit his farm, he noticed that the rope and some spikes are missing. Your task is to write a program to help Kamran decide whether the boundary of his farm can be exactly determined only by the remaining spikes.

Input

The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (1 <= n <= 1000) which is the number of remaining spikes. Next, there are n lines, one line per spike, each containing a pair of integers which are x and y coordinates of the spike.

Output

There should be one output line per test case containing YES or NO depending on whether the boundary of the farm can be uniquely determined from the input.

思路:

 求凸包,然后判断每个线段上至少有三个点,则输出yes,否则no;

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
const double eps = 1e-10;
int dcmp(double x)
{
    if (fabs(x) < eps) return 0;
    return x < 0 ? -1 : 1;
}
struct Point
{
    double x, y;
    Point() {}
    Point(double x, double y) :x(x), y(y) {}
    bool operator==(const Point& B)const
    {
        return dcmp(x - B.x) == 0 && dcmp(y - B.y) == 0;
    }
    bool operator<(const Point& B)const
    {
        return dcmp(x - B.x) < 0 || (dcmp(x - B.x) == 0 && dcmp(y - B.y) < 0);
    }
};
typedef Point Vector;
Vector operator-(Point A, Point B)
{
    return Vector(A.x - B.x, A.y - B.y);
}
double Cross(Vector A, Vector B)
{
    return A.x * B.y - A.y * B.x;
}
int ConvexHull_1(Point* p, int n, Point* ch)//点数最少的凸包
{//该凸包的点集任意3点不会共线
    sort(p, p + n);
    n = unique(p, p + n) - p;
    int m = 0;
    for (int i = 0; i < n; i++)
    {
        while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;//注意<=0
        ch[m++] = p[i];
    }
    int k = m;
    for (int i = n - 2; i >= 0; i--)
    {
        while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
        ch[m++] = p[i];
    }
    if (n > 1) m--;
    return m;
}
int ConvexHull_2(Point* p, int n, Point* ch)//点数最多的凸包
{//该凸包的点集ch中可能相邻3点共线
    sort(p, p + n);
    n = unique(p, p + n) - p;
    int m = 0;
    for (int i = 0; i < n; i++)
    {
        while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) < 0) m--;//注意<0
        ch[m++] = p[i];
    }
    int k = m;
    for (int i = n - 2; i >= 0; i--)
    {
        while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) < 0) m--;
        ch[m++] = p[i];
    }
    if (n > 1) m--;
    return m;
}
bool check(Point* ch1, int m1, Point* ch2, int m2)//判定凸包是否稳定
{//判定ch1点集中的任意相邻两点构成的边之间是否存在另外一个点
    int j;
    for (j = 0; j < m2; j++)
        if (ch1[0] == ch2[j]) break;
    for (int i = 0; i < m1; i++)
    {
        bool flag = false;
        j = (j + 1) % m2;
        while (!(ch2[j] == ch1[(i + 1) % m1]))
        {
            j = (j + 1) % m2;
            flag = true;
        }
        if (flag == false) return false;
    }
    return true;
}
const int maxn = 1000 + 5;
Point p[maxn], ch1[maxn], ch2[maxn];
int main()
{
    int T; scanf("%d", &T);
    while (T--)
    {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
            scanf("%lf%lf", &p[i].x, &p[i].y);
        if (n < 6)
        {
            printf("NO\n");
            continue;
        }
        int m1 = ConvexHull_1(p, n, ch1);
        int m2 = ConvexHull_2(p, n, ch2);

        bool ok = check(ch1, m1, ch2, m2);
        printf("%s\n", ok ? "YES" : "NO");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值