poj3304-Segments 是否存在直线经过所有线段
来源:互联网 发布:淘宝助理下载宝贝 编辑:程序博客网 时间:2024/06/05 15:10
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
321.0 2.0 3.0 4.04.0 5.0 6.0 7.030.0 0.0 0.0 1.00.0 1.0 0.0 2.01.0 1.0 2.0 1.030.0 0.0 0.0 1.00.0 2.0 0.0 3.01.0 1.0 2.0 1.0
Sample Output
Yes!Yes!No!
题目大意:给你n个线段,询问是否存在一条直线经过这些线段,即与这些线段都有公共点。
直接枚举所有端点构成的直线,是否存在直线满足条件,存在输出yes,否则输出no。
注意枚举点的时候要去掉距离小于1e-8的两点,否则答案不正确。
ac代码如下:
#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<stdlib.h>using namespace std;const double eps = 1e-8;const double PI = acos(-1.0);int sgn(double x){if(fabs(x) < eps)return 0;if(x < 0)return -1;else return 1;}struct Point {double x,y;Point() {}Point(double _x,double _y) {x = _x;y = _y;}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;}//点积double operator *(const Point &b)const {return x*b.x + y*b.y;}//绕原点旋转角度B(弧度值),后x,y的变化void transXY(double B) {double tx = x,ty = y;x = tx*cos(B) - ty*sin(B);y = tx*sin(B) + ty*cos(B);}};struct Line {Point s,e;Line() {}Line(Point _s,Point _e) {s = _s;e = _e;}//两直线相交求交点//第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交//只有第一个值为2时,交点才有意义pair<int,Point> operator &(const Line &b)const {Point res = s;if(sgn((s-e)^(b.s-b.e)) == 0) {if(sgn((s-b.e)^(b.s-b.e)) == 0)return make_pair(0,res);//重合else return make_pair(1,res);//平行}double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));res.x += (e.x-s.x)*t;res.y += (e.y-s.y)*t;return make_pair(2,res);}};double dist(Point a,Point b){return sqrt((a-b)*(a-b));}bool Seg_inter_line(Line l1,Line l2) //判断直线l1和线段l2是否相交{return sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0;}int n;Line L[105];bool fun2(Line l1){if(dist(l1.s,l1.e)<eps) return false;int i;for(i=0;i<n;i++) if(!Seg_inter_line(l1,L[i])) return false; if(i==n) return true;}bool fun(Line l1,Line l2){Line q;q.s=l1.s; q.e=l2.s;if(fun2(q)) return true;q.s=l1.e; q.e=l2.s;if(fun2(q)) return true;q.s=l1.s; q.e=l2.e;if(fun2(q)) return true;q.s=l1.e; q.e=l2.e;if(fun2(q)) return true;return false;}int main(){int t;scanf("%d",&t);while(t--){ int i,j; int flag=0;scanf("%d",&n);for(i=0;i<n;i++) scanf("%lf%lf%lf%lf",&L[i].s.x,&L[i].s.y,&L[i].e.x,&L[i].e.y);if(n<3) flag=1;else{for(i=0;i<n;i++){for(j=0;j<n;j++){if(fun(L[i],L[j])) { flag=1; break; }}if(flag) break;}}if(flag) printf("Yes!\n");else printf("No!\n");}return 0;}题目网址:点击打开链接http://poj.org/problem?id=3304
- poj3304-Segments 是否存在直线经过所有线段
- poj3304 判断是否存在一条直线经过n条线段
- Segments----是否存在直线与所有线段相交
- poj3304-Segments-判断直线和线段是否相交
- poj 3304 Segments 是否存在一条穿过所有线段的直线
- poj 3304 Segments 【判断是否存在一条直线与所有线段相交】
- Segments(poj3304,判断线段与直线相交)
- POJ3304 Segments(计算几何,线段和直线的交点)
- 判断是否存在一条直线穿过所有的线段
- poj3304——Segments(判断直线与多个线段相交)
- POJ 3304:Segments 计算几何 是否有直线与所有线段相交
- poj3304 Segments(计算几何+直线相交)
- POJ3304-线段与直线相交
- poj3304求是否存在一条直线可以与各个线段有交点
- poj 3304 Segments (跟所有线段相交的直线)
- POJ 3304 Segments (判断直线和线段是否相交)
- POJ 3304 Segments(判断线段和直线是否相交)
- poj 3304 Segments(贪心+直线是否与线段相交!)
- css引用的方式和区别性
- Java使用Protocol Buffers入门四步骤
- 服务器主逻辑行列
- 常见问题
- CF #406 div1 ABC
- poj3304-Segments 是否存在直线经过所有线段
- Oracle执行计划详细解读
- 【JavaSE学习笔记】面向对象_01(入门,匿名对象,成员变量,局部变量,封装,this,构造方法)
- android studio中对于jre版本选择问题
- XL5python下使用opencv
- kotlin 和java 混编
- 微型四轴DIY机架,轻巧稳固耐摔,通用720空心杯电机,9厘米轴距
- Bootstrap项目实战——架构视角介绍使用
- VLAN间的相互通信(S5720)