题目传送门:POJ 3304 Segments

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

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!

题目大意:

  在一个平面上,给你n个线段,问是否存在一条直线,使得将这些线段投影到上面之后,所有投影线段至少有一个共同点。

解题思路:

  若存在一条直线使得所有线段投影到上面之后其投影至少有一个公共点,则这条直线必有一条垂线与所有线段都相交。

  这题就转化为了求是否存在一条直线与所有线段都相交。

  接下来我们只需枚举两个端点(不是同一直线上的)来找这条直线,若所有端点对的直线都不经过所有线段,则不存在这样的直线。

  我们的重点就是判断直线与线段是否相交。同时需要注意如果枚举的两个端点相等则为一个点不能确定直线和精度问题(题目说了是10-8)。

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define eps 1e-8
using namespace std;
const int N = + ;
int dcmp(double x)
{
if(fabs(x)<eps) return ;
return x<?-:;
}
struct point ///点
{
double x, y;
point(double a = , double b = ){ x = a, y = b;}
point operator-(const point& b) const { return point(x - b.x, y - b.y);}
double operator^(const point& b) const { return x * b.y - y * b.x; }
} p[*N];
bool operator == (const point& a, const point& b)
{
return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ;
}
struct line ///线
{
point s, e;
} l[N]; bool Line_Seg(point a1,point a2,line l)
{
double c1=((a1-a2)^(l.s-a2)),c2=((a1-a2)^(l.e-a2));
return dcmp(c1)*dcmp(c2)<=;
}
int num,n,T;
bool judge()
{
for (int i=; i<num; i++)
for (int j=i+,k; j<num; j++)
{
if (p[i]==p[j]) continue;
for ( k=; k<n; k++)
if (!Line_Seg(p[i],p[j],l[k]))
break;
if (k>=n) return ;
}
return ;
} int main()
{
for ( scanf("%d",&T); T; T--)
{
num=;
scanf("%d",&n);
for (int i=; i<n; i++)
{
scanf("%lf%lf%lf%lf",&l[i].s.x,&l[i].s.y,&l[i].e.x,&l[i].e.y);
p[num++]=l[i].s;
p[num++]=l[i].e;
}
if (judge()) printf("Yes!\n");
else printf("No!\n");
}
return ;
}

POJ 3304 Segments(判断直线与线段是否相交)的更多相关文章

  1. POJ 3304 Segments 判断直线和线段相交

    POJ 3304  Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以 ...

  2. POJ 3304 Segments (判断直线与线段相交)

    题目链接:POJ 3304 Problem Description Given n segments in the two dimensional space, write a program, wh ...

  3. POJ 3304 Segments (直线与线段是否相交)

    题目链接 题意 : 能否找出一条直线使得所有给定的线段在该直线上的投影有一个公共点. 思路 : 假设存在一条直线a使得所有线段在该直线上的投影有公共点,则必存在一条垂直于直线a的直线b,直线b与所有线 ...

  4. POJ 3304 Segments (直线和线段相交判断)

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7739   Accepted: 2316 Descript ...

  5. [poj] 3304 Segments || 判断线段相交

    原题 给出n条线段,判断是否有一条直线与所有线段都有交点 若存在这样一条直线,那么一定存在一条至少过两个线段的端点的直线满足条件. 每次枚举两条线段的两个端点,确定一条直线,判断是否与其他线段都有交点 ...

  6. Segments - POJ 3304 (判断直线与线段是否相交)

    题目大意:给出一些线段,然后判断这些线段的投影是否有可能存在一个公共点.   分析:如果这些线段的投影存在一个公共点,那么过这个公共点作垂线一定与所有的直线都想交,于是题目转化成是否存在一个直线可以经 ...

  7. POJ 3304 Segments(直线)

    题目: Description Given n segments in the two dimensional space, write a program, which determines if ...

  8. 简单几何(线段与直线的位置) POJ 3304 Segments

    题目传送门 题意:有若干线段,问是否存在一条直线,所有线段投影到直线上时至少有一个公共点 分析:有一个很好的解题报告:二维平面上线段与直线位置关系的判定.首先原问题可以转换为是否存在一条直线与所有线段 ...

  9. 判断直线与线段相交 POJ 3304 Segments

    题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...

随机推荐

  1. laravel asset()函数

    asset() 使用当前请求的scheme(HTTP或HTTPS)为前端资源生成一个URL: $url = asset('img/photo.jpg'); laravel自带了laravel-mix, ...

  2. 求解最长回文串 manachar算法

    转载:http://blog.sina.com.cn/s/blog_70811e1a01014esn.html ;i<len;++i){ if(mx>i) p[i]=min(p[*id-i ...

  3. while循环计算1-100和,1-100内偶数/奇数/被整除的数的和

    文章地址 https://www.cnblogs.com/sandraryan/ <!DOCTYPE html> <html lang="en"> < ...

  4. 2019-1-29-WPF-设置输入只能英文

    title author date CreateTime categories WPF 设置输入只能英文 lindexi 2019-1-29 15:8:4 +0800 2018-2-13 17:23: ...

  5. java 获得Class对象

    如何得到各个字节码对应的实例对象? 每个类被加载后,系统会为该类生成对应的Class对象,通过Class对象可以访问到JVM中的这个类, 3种方式: 1.调用某个类的class属性获取Class对象, ...

  6. P1049 找第K大的数

    题目描述 给定一个无序正整数序列, 以及另一个数n (1<=n<=1000000), 然后以类似快速排序的方法找到序列中第n大的数(关于第n大的数:例如序列{1,2,3,4,5,6}中第3 ...

  7. H3C使用ping命令--用户视图

    完整的ping命令,用于下面参考 <H3C>ping 1.1.1.1   PING 1.1.1.1: 56  data bytes, press CTRL_C to break     R ...

  8. 土旦:移动端 Vue+Vant 的Uploader 实现 :上传、压缩、旋转图片

    面向百度开发 html <van-uploader :after-read="onRead" accept="image/*"> <img s ...

  9. CF Round #580(div2)题解报告

    CF Round #580(div2)题解报告 T1 T2 水题,不管 T3 构造题,证明大约感性理解一下 我们想既然存在解 \(|a[n + i] - a[i]| = 1\) 这是必须要满足的 既然 ...

  10. 一个vue管理系统的初步搭建总结

    ps:目前这个项目只是有一个大致的框架,并没有做完 前期准备工作 前端构建工具:Visual Studio Code后端构建工具:IDEA数据库和服务器构建工具:WampServer (使用的是2.4 ...