题目传送门: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. 2018-2-13-win10-uwp-获得Slider拖动结束的值

    title author date CreateTime categories win10 uwp 获得Slider拖动结束的值 lindexi 2018-2-13 17:23:3 +0800 201 ...

  2. Google Colab——用谷歌免费GPU跑你的深度学习代码

    Google Colab简介 Google Colaboratory是谷歌开放的一款研究工具,主要用于机器学习的开发和研究.这款工具现在可以免费使用,但是不是永久免费暂时还不确定.Google Col ...

  3. 2013-10-6 datagridview实现换行并自动设置行高

    datagridview设置换行,如下,文本设置\r\n即可换行 dv4.DefaultCellStyle.WrapMode = DataGridViewTriState.True; dv4.Auto ...

  4. Python--day48--ORM框架SQLAlchemy操作表

    ORM框架SQLAlchemy操作表: 表结构和数据库连接: #!/usr/bin/env python # -*- coding:utf-8 -*- from sqlalchemy.ext.decl ...

  5. hadoop 端口总结

    localhost:50030/jobtracker.jsp localhost:50060/tasktracker.jsp localhost:50070/dfshealth.jsp 1. Name ...

  6. hadoop的6个进程启动不全,请试 比如datanode没有启动

      赋给hadoop用户hadoop-1.2.1读写权限 [root@master usr]# chown -R hadoop121:hadoop121 hadoop-1.2.1/   折腾了两个小时 ...

  7. SELECT command denied to user 'username'@'ip' for table 'user'错误处理

    错误信息 使用RDS for MySQL,程序执行查询SQL时报错如下: SELECT command denied to user 'username'@'ip' for table 'user' ...

  8. Java 5,6,7,8,9,10,11新特性吐血总结

    作者:拔剑少年 简书地址:https://www.jianshu.com/u/dad4d9675892 博客地址:https://it18monkey.github.io 转载请注明出处 java5 ...

  9. Springboot上传文件临时目录无效

    一个奇葩问题,虽然解决了,但还是没弄清楚,小记一笔. 年后回来,测试人员对年前的3次迭代的功能进行了回归测试,然后发现所有excel导入的功能都失效了.作为后台开发人员,当然是第一时间打开运行日志排查 ...

  10. H3C 端口隔离配置举例