poj 3304 Segments(线段与直线相交)

来源:互联网 发布:win10激活工具 知乎 编辑:程序博客网 时间:2024/05/21 17:52

http://poj.org/problem?id=3304

Segments
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11996 Accepted: 3789

Description

Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.

Input

Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1y1) and (x2y2) are the coordinates of the two endpoints for one of the segments.

Output

For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.

Sample Input

321.0 2.0 3.0 4.04.0 5.0 6.0 7.030.0 0.0 0.0 1.00.0 1.0 0.0 2.01.0 1.0 2.0 1.030.0 0.0 0.0 1.00.0 2.0 0.0 3.01.0 1.0 2.0 1.0

Sample Output

Yes!Yes!No!

题意:给定n条线段,确定是否存在一条直线,使得这n条线段在这条直线上的射影具有公共点

可将问题转化为是否存在一条直线经过所有的线段

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<cmath>using namespace std;#define N 5110#define INF 0x3f3f3f3f#define MOD#define EPS 1e-8#define met(a, b) memset(a, b, sizeof(a))typedef long long LL;using namespace std;struct point{    double x, y;};struct Line{    point a, b;};Line p[N];int n;double xmult (point p1, point p2, point p0)///叉积{    return (p1.x-p0.x) * (p2.y-p0.y) - (p2.x-p0.x) * (p1.y-p0.y);}double Dis (point a, point b)///判断两线段端点是否重点{    return sqrt ((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}bool Judge (point a, point b)///判断两点的连线和其他所有线段是否有交点{    if (Dis (a, b) < EPS) return false;///判断两线段端点是否重点    for (int i=1; i<=n; i++)        if (xmult (p[i].a, a, b) * (xmult (p[i].b, a, b)) > EPS) return false;    return true;}int main (){    int T;    scanf ("%d", &T);    while (T--)    {        met (p, 0);        scanf ("%d", &n);        bool flag = false;        for (int i=1; i<=n; i++)            scanf ("%lf%lf%lf%lf", &p[i].a.x, &p[i].a.y, &p[i].b.x, &p[i].b.y);        for (int i=1; i<=n; i++)        {            if (Judge (p[i].a, p[i].b)) flag = true;            for (int j=i+1; j<=n && !flag; j++)            {                if (Judge (p[i].a, p[j].a) || Judge (p[i].a, p[j].b)                    || Judge (p[i].b, p[j].a) || Judge (p[i].b, p[j].b))                    {                        flag = true;                        break;                    }            }            if (flag) break;        }        if (flag) puts ("Yes!");        else puts ("No!");    }    return 0;}


0 0