Poj3304 Segments

来源:互联网 发布:大数据 视频 编辑:程序博客网 时间:2024/05/16 15:36

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 x1y1 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条线段在这条直线上的投影具有公共点。

1、把问题转化为是否存在一条直线与每条线段都有交点。证明:若存在一条直线l和所有线段相交,作一条直线m和l垂直,则m就是题中要求的直线,所有线段投影的一个公共点即为垂足。

2、枚举两两线段的各一个端点,连一条直线,再判断剩下的线段是否都和这条直线有交点。证明:若有l和所有线段相交,则可保持l和所有线段相交,左右平移l到和某一线段交于端点停止(“移不动了”)。然后绕这个交点旋转。也是转到“转不动了”(和另一线段交于其一个端点)为止。这样就找到了一个新的l满足题意,而且经过其中两线段的端点。

代码:

#include<iostream>#include<cmath>using namespace std;const int Max = 105;const double eps = 1e-8; struct Point{    double x, y;}s[Max], e[Max];int n; double mult(Point p1, Point p2, Point p0) {    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);} bool run(Point p1, Point p2){    if(abs(p1.x-p2.x) < eps && abs(p1.y-p2.y) < eps)         return false;    for(int i = 0; i < n; i ++)        if(mult(p1, p2, s[i]) * mult(p1, p2, e[i]) > eps) return false;    return true;} int main(){    int t;    cin >> t;    for(int Z = 1; Z <= t; Z ++)    {        cin >> n;        for(int i = 0; i < n; i ++)            cin >> s[i].x >> s[i].y >> e[i].x >> e[i].y;        bool flag = false;        if(n < 3) flag = true;        for(int i = 0; i < n && !flag; i ++)            for(int j = i + 1; j < n && !flag; j ++)            {                if(run(s[i], s[j])) flag = true;                else if(run(s[i], e[j])) flag = true;                else if(run(e[i], s[j])) flag = true;                else if(run(e[i], e[j])) flag = true;            }           if(flag) cout << "Yes!" << endl;           else cout << "No!" << endl;    }    return 0;}



0 0
原创粉丝点击