Segments
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10921   Accepted: 3422

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

  1. 3
  2. 2
  3. 1.0 2.0 3.0 4.0
  4. 4.0 5.0 6.0 7.0
  5. 3
  6. 0.0 0.0 0.0 1.0
  7. 0.0 1.0 0.0 2.0
  8. 1.0 1.0 2.0 1.0
  9. 3
  10. 0.0 0.0 0.0 1.0
  11. 0.0 2.0 0.0 3.0
  12. 1.0 1.0 2.0 1.0

Sample Output

  1. Yes!
  2. Yes!
  3. No!

Source

题意:t组数据,每组n个线段,都是给定四个点表,问是否存在一条直线使所有线段在这个直线上的投影互相至少相交于一点.

思路:转化成存在一条直线与所有线段都相交。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <math.h>
  7. #include <algorithm>
  8. #include <cctype>
  9. #include <string>
  10. #include <map>
  11. #include <set>
  12. #define ll long long
  13. using namespace std;
  14. const double eps = 1e-;
  15. int sgn(double x)
  16. {
  17. if(fabs(x) < eps)return ;
  18. if(x < ) return -;
  19. else return ;
  20. }
  21. struct Point
  22. {
  23. double x,y;
  24. Point(){}
  25. Point(double _x,double _y)
  26. {
  27. x = _x;y = _y;
  28. }
  29. Point operator -(const Point &b)const
  30. {
  31. return Point(x - b.x,y - b.y);
  32. }
  33. double operator ^(const Point &b)const
  34. {
  35. return x*b.y - y*b.x;
  36. }
  37. double operator *(const Point &b)const
  38. {
  39. return x*b.x + y*b.y;
  40. }
  41. };
  42. struct Line
  43. {
  44. Point s,e;
  45. Line(){}
  46. Line(Point _s,Point _e)
  47. {
  48. s = _s;e = _e;
  49. }
  50. };
  51. double xmult(Point p0,Point p1,Point p2) //p0p1 X p0p2
  52. {
  53. return (p1-p0)^(p2-p0);
  54. }
  55. bool Seg_inter_line(Line l1,Line l2) //判断直线l1和线段l2是否相交
  56. {
  57. return sgn(xmult(l2.s,l1.s,l1.e))*sgn(xmult(l2.e,l1.s,l1.e)) <= ;
  58. }
  59. double dist(Point a,Point b)
  60. {
  61. return sqrt( (b - a)*(b - a) );
  62. }
  63. const int MAXN = ;
  64. Line line[MAXN];
  65. bool check(Line l1,int n)
  66. {
  67. if(sgn(dist(l1.s,l1.e)) == ) return false;
  68. for(int i = ; i < n; i++)
  69. if(Seg_inter_line(l1,line[i]) == false) return false;
  70. return true;
  71. }
  72.  
  73. int main(void)
  74. {
  75. int n,t;
  76. scanf("%d",&t);
  77. while(t--)
  78. {
  79. scanf("%d",&n);
  80. double x1,y1,x2,y2;
  81. for(int i = ; i < n; i++)
  82. {
  83. scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
  84. line[i] = Line(Point(x1,y1),Point(x2,y2));
  85. }
  86. bool flag = false;
  87. for(int i = ; i < n; i++)
  88. for(int j = ; j < n; j++)
  89. if(check(Line(line[i].s,line[j].s),n) || check(Line(line[i].s,line[j].e),n) || check(Line(line[i].e,line[j].s),n)||check(Line(line[i].e,line[j].e),n) )
  90. {
  91. flag = true;
  92. break;
  93. }
  94.  
  95. if(flag) printf("Yes!\n");
  96. else printf("No!\n");
  97. }
  98. return ;
  99. }

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 (直线和线段相交判断)

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

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

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

  5. Segments--poj3304(判断直线与线段之间的关系)

    http://poj.org/problem?id=3304 给你几条线段  然后 让你找到一条直线让他在这条直线上的映射有一个重合点 如果有这条直线的话  这个重合的部分的两个端点一定是某两条线段的 ...

  6. POJ 3304 Segments(直线)

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

  7. POJ 3304 Segments(计算几何:直线与线段相交)

    POJ 3304 Segments 大意:给你一些线段,找出一条直线可以穿过全部的线段,相交包含端点. 思路:遍历全部的端点,取两个点形成直线,推断直线是否与全部线段相交,假设存在这种直线,输出Yes ...

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

    题目传送门:POJ 3304 Segments Description Given n segments in the two dimensional space, write a program, ...

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

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

随机推荐

  1. LOJ#6075. 「2017 山东一轮集训 Day6」重建

    题目描述: 给定一个 n个点m 条边的带权无向连通图 ,以及一个大小为k 的关键点集合S .有个人要从点s走到点t,现在可以对所有边加上一个非负整数a,问最大的a,使得加上a后,满足:s到t的最短路长 ...

  2. 使用vue-cli3快速适配H5项目

    跟我老大学到了一招使用vue-cli3快速适配H5项目的方法. 我之前也有进行一个版本的适配,直接使用cnpm install -g vue-cli,然后安装各种插件进行适配,见我之前的博客. 后来老 ...

  3. WPF 多语言

    1.http://www.cnblogs.com/bear831204/archive/2009/03/17/1414026.html 2.http://www.cnblogs.com/horan/a ...

  4. 洛谷P1129 【ZJOI2007】矩阵游戏

    原题传送门 题目描述 小QQ是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏――矩阵游戏.矩阵游戏在一个N \times NN×N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每 ...

  5. 修改cmd命令默认路径

    未修改之前: 修改方法: 1.win+r打开运行对话框,输入 regedit 打开注册表编辑器 2.在注册表中找到:HKEY_CURRENT_USER\Software\Microsoft\Comma ...

  6. [洛谷P2472] [SCOI2007]蜥蜴

    题目链接: 蜥蜴 题目分析: 一道网络流,先来分析一下问题: 在一个\(r*c\)的图中分布了一些数,其他地方都用\(0\)填充,我们分别从指定的一些数出发,每次可以移动到周围距离为\(d\)以内的数 ...

  7. 怎样理解js数组中indexOf()的用法与lastIndexOf

    第一首先你运行一下它的js代码: var arr1=["大学","中庸","论语","孟子","诗" ...

  8. PKU--2184 Cow Exhibition (01背包)

    题目http://poj.org/problem?id=2184 分析:给定N头牛,每头牛都有各自的Si和Fi 从这N头牛选出一定的数目,使得这些牛的 Si和Fi之和TS和TF都有TS>=0 F ...

  9. 获取计算机以及本机信息API

    获取计算机名: BOOL GetComputerName( LPTSTR lpBuffer, // computer name LPDWORD lpnSize // size of name buff ...

  10. 安装postgresql11.5

    root身份安装 创建用户 编译安装成功后,接下来要做的就是创建一个普通用户,因为默认超级用户(root)不能启动postgresql,所以需要创建一个普通用户来启动数据库,执行以下命令创建用户: [ ...