/*
凸包相交
对于不能形成凸包的点集可以对线段进行叉积,从而判断两个点集是否相交
*/
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
double eps = 1e-15;
double pi = acos(-1);
struct Point{
double x, y;
Point(double x = 0, double y = 0) :x(x), y(y) {}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } //两向量(或两点)相加
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } //两向量(或两点)相减
Vector operator * (Vector A, double B) { return Vector(A.x*B, A.y*B); } //将向量A(或点A)x,y坐标均加上B
Vector operator / (Vector A, double B) { return Vector(A.x / B, A.y / B); } //将向量B(或点B)x,y坐标均减去B
int dcmp(double x) {
if (fabs(x) < eps)return 0;
else return x < 0 ? -1 : 1;
}
bool operator < (const Point &a, const Point &b) {
return dcmp(a.x - b.x) < 0 || (dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) < 0);
}
bool operator == (const Point &a, const Point &b) {
return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Cross(Vector A, Vector B) { //两向量求叉积
return A.x*B.y - A.y*B.x;
}
double Dot(Vector A, Vector B) { //两向量求点积
return A.x*B.x + A.y*B.y;
}
Vector Rotate(Vector A, double rad) { //向量翻转
return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
}
int tubao(Point *p, int n, Point *ch) { //建立凸包
sort(p, p + n);
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--;
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;
}
void readp(Point &A,double x1,double x2) {
A.x = x1;
A.y = x2;
}
bool onsegment(Point p, Point a1, Point a2) { //判断点是否在线段上
if (p == a1 || p == a2)return false;
return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0; //dcmp(Cross(a1 - p, a2 - p)) == 0判断a1,a2是否与p共线,dcmp(Dot(a1 - p, a2 - p)) < 0p是否在线段中间
}
bool segmentcross(Point a1, Point a2, Point b1, Point b2) { //判断是否存在线段相交
if (a1 == b1 || a1 == b2 || a2 == b1 || a2 == b2)return true;
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}
int intubao(Point *ch, int n, Point p) {
Vector A, B;
int flag = 0;
for (int i = 0; i < n; i++) {
A = ch[(i + 1) % n] - ch[i];
B = p - ch[i];
if (onsegment(p, ch[i], ch[(i + 1) % n])) { //判断点p是否在凸包外围
flag = -1;
break;
}
else if (Cross(A, B) > 0) { //如果点p在凸包内,对于所有凸包外围的线段都有Cross(A,B)>0;
flag++;
}
}
if (flag == -1 || flag == n)return 1;
return 0;
}
int T, n, m;
Point p1[N], ch1[N], p2[N], ch2[N];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
int cnt1, cnt2;
cnt1 = cnt2 = 0;
for (int i = 1; i <= n; i++) {
double x1, x2;
int f;
scanf("%lf%lf%d", &x1, &x2, &f);
if (f == 1)readp(p1[cnt1++], x1, x2);
else if (f == -1)readp(p2[cnt2++], x1, x2);
}
int len1=tubao(p1, cnt1, ch1);
int len2=tubao(p2, cnt2, ch2);
bool flag = true;
for (int i = 0; i < len2; i++) {
if (intubao(ch1, len1, ch2[i]))
{
flag = false;
break;
}
}
if (flag) {
for (int i = 0; i < len1; i++) {
if (intubao(ch2, len2, ch1[i]))
{
flag = false;
break;
}
}
}
if (flag) {
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
if (segmentcross(ch1[(i + 1) % len1], ch1[i], ch2[(j + 1) % len2], ch2[j]))flag = false;
if (!flag)break;
}
if (!flag)break;
}
}
printf(flag ? "Successful!\n" : "Infinite loop!\n");
}
return 0;
}
HDU 6590
最新推荐文章于 2019-08-15 19:05:29 发布