码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3304:Segments——题解

时间:2017-12-18 19:13:41      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:log   blog   mat   abs   segment   body   直线   https   投影   

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

题目大意:给n条线段,求是否存在一条直线,将所有线段投影到上面,使得所有投影至少交于一点。

————————————————————————————

首先考虑当情况可能时,过相交点做垂线,则垂线一定与所有线相交。

所以就变成了求是否存在一条直线,使得直线和所有直线都相交的问题了。

显然如果存在这样的线,那么至少有一种情况,这样的线的两个端点是其中两条直线的任意两个端点。

那么枚举两个端点判断即可。

https://www.cnblogs.com/wuwangchuxin0924/p/6218494.html 如何判断两直线相交。

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
typedef double dl;
const dl eps=1e-8;
const int N=101;
struct point{//既是向量又是点
    dl x;
    dl y;
}p[2*N];
int n;
inline point getmag(point a,point b){
    point s;
    s.x=b.x-a.x;s.y=b.y-a.y;
    return s;
}
inline dl multiX(point a,point b){
    return a.x*b.y-b.x*a.y;
}
bool check(point a,point b){
    if(fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps)return 0;  
    for(int i=1;i<=n;i++){  
    if(multiX(getmag(a,p[i]),getmag(a,b))*multiX(getmag(a,p[i+n]),getmag(a,b))>eps)return 0;
    }
    return 1;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i+n].x,&p[i+n].y);
    }
    bool flag=0;
    for(int i=1;i<=2*n&&!flag;i++){
        for(int j=i+1;j<=2*n&&!flag;j++){
        if(check(p[i],p[j]))flag=1;
        }
    }
    if(flag)puts("Yes!");
    else puts("No!");
    }
    return 0;
}

 

POJ3304:Segments——题解

标签:log   blog   mat   abs   segment   body   直线   https   投影   

原文地址:http://www.cnblogs.com/luyouqi233/p/8057652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!