标签:printf targe getline string define connected sizeof cti 传送门
题目:传送门
题意: 给你 n 条线段的两个端点, 然后有多次询问, 每次询问, 问你线段 x 和 线段 y 是否相交。
若线段 A 和线段 B 相交且线段 A 和线段 C 相交,那么线段 B 和线段 C 相交。
1 < n < 13
题解: 暴力求线段是否相交, 然后再跑个 Floyd 或者并查集都可以的。
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <map> #include <vector> #include <set> #include <string> #include <math.h> #define LL long long #define mem(i, j) memset(i, j, sizeof(i)) #define rep(i, j, k) for(int i = j; i <= k; i++) #define dep(i, j, k) for(int i = k; i >= j; i--) #define pb push_back #define make make_pair #define INF INT_MAX #define inf LLONG_MAX #define PI acos(-1) using namespace std; const int N = 310; 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 - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); } ///向量*数=向量 Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); } ///向量/数=向量 Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); } const double eps = 1e-10; 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 a.x == b.x ? a.y < b.y : a.x < b.x; } bool operator == (const Point& a, const Point &b) { return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; } double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } /// 点积 double Length(Vector A) { return sqrt(Dot(A, A)); } /// 计算向量长度 double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角 double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) { /// 求两直线交点,请确保 P + tv 和 Q + tw 有唯一交点。 当且仅当Cross(v, w)非0, v,w为直线的方向向量, P, Q为直线上任一点 Vector u = P - Q; double t = Cross(w, u) / Cross(v, w); return P + v * t; } bool OnSegment(Point p, Point a1, Point a2) { /// 点p是否在线段a1,a2上(包含端点) return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) <= 0; } bool SegmentProperInsection(Point a1, Point a2, Point b1, Point b2) {/// 判断线段是否相交,包括端点 if(dcmp(Cross(a1 - a2, b1 - b2)) == 0) return OnSegment(b1, a1, a2) || OnSegment(b2, a1, a2) || OnSegment(a1, b1, b2) || OnSegment(a2, b1, b2); else { Point t = GetLineIntersection(a1, a2 - a1, b1, b2 - b1); return OnSegment(t, a1, a2) && OnSegment(t, b1, b2); } } Point P[100], Q[100]; bool ans[100][100]; int main() { int n; while(scanf("%d", &n) == 1 && n ) { rep(i, 1, n) { scanf("%lf %lf %lf %lf", &P[i].x, &P[i].y, &Q[i].x, &Q[i].y); } mem(ans, 0); rep(i, 1, n) { ans[i][i] = 1; rep(j, 1, i - 1) { ans[i][j] = ans[j][i] = SegmentProperInsection(P[i], Q[i], P[j], Q[j]); } } rep(i, 1, n) rep(j, 1, n) rep(k, 1, n) ans[j][k] |= ans[j][i] && ans[i][k]; int x, y; while(scanf("%d %d", &x, &y)) { if(x + y == 0) break; if(ans[x][y]) printf("CONNECTED\n"); else printf("NOT CONNECTED\n"); } } return 0; }
POJ 1127 Jack Straws ( 求直线交点, 判断线段是否相交(含端点) )
标签:printf targe getline string define connected sizeof cti 传送门
原文地址:https://www.cnblogs.com/Willems/p/12341713.html