Problem
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.
Sample Input
1
6
0 0
1 2
3 4
2 0
2 4
5 0
Sample Output
NO
分析
先引用一个大神的分析:https://blog.csdn.net/u013480600/article/details/40055413
我最初没有真正理解约束条件:每个边上至少是3个点。
- 一开始就是分成冗余凸包和最小凸包比较点数。
- 后来才想到,原来是每个边上还要有一个点,结果还是wa:因为当然可以大于三个点啊!!!
以为自己两种convex hull计算写错了,浪费了好多时间!
有时间再自己摸一遍吧。
代码
引用:https://blog.csdn.net/u013480600/article/details/40055413
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
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;
}