POJ 3304 segments (直线与线段相交)

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9837 Accepted: 3030

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 (x1, y1) and (x2, y2) 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

3
2
1.0 2.0 3.0 4.0
4.0 5.0 6.0 7.0
3
0.0 0.0 0.0 1.0
0.0 1.0 0.0 2.0
1.0 1.0 2.0 1.0
3
0.0 0.0 0.0 1.0
0.0 2.0 0.0 3.0
1.0 1.0 2.0 1.0

Sample Output

Yes!
Yes!
No!

题意:题中给出一些线段,问这些线段在某一个方向上的投影是否能存在公共点;

题解:
很容易想到,要是在某个方向上投影能够存在公共点的话,那么必存在一条直线可以与所有的直线相交,另外弱国存在该直线的话,该直线经过一定旋转后必存在两点是原线段中的某两点,那么该题就转化为枚举直线看是否存在一条直线能和所有的线段相交。

题目链接:http://poj.org/problem?id=3304

判断直线与线段相交的方法:利用叉积

代码:
/**********
    Problem: 3304		User: pluto227
    Memory: 160K		Time: 32MS
    Language: C++		Result: Accepted
**********/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#define eps 1e-8
#define LL long long
using namespace std;

const int N = 200;

struct Point {
    double x;
    double y;
} point[N];

inline double cross(Point a,Point b,Point c) {
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

inline double dist(double x1,double y1,double x2,double y2) {
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

inline bool judge(Point a,Point b,int n) {
    if((dist(a.x,a.y,b.x,b.y))<eps) return false;
    for(int t=1; t<=n; t++) {
        int mk=2*t-1;
        if(cross(a,b,point[mk])*cross(a,b,point[mk+1])>eps) return false;
    }
    return true;
}

int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        int n,k=1;
        scanf("%d",&n);
        for(int i=0; i<n; i++) {
            scanf("%lf %lf",&point[k].x,&point[k].y);
            k++;
            scanf("%lf %lf",&point[k].x,&point[k].y);
            k++;
        }
        bool ans=false;
        if(n==1) {
            printf("Yes!\n");
            continue;
        }
        for(int i=1; i<=k-1; i++) {
            for(int j=i+1; j<=k-1; j++) {
                if(judge(point[i],point[j],n)){
                    ans=true;
                }
            }
        }
        if(ans) printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值