poj 3304 判断是否存在一条直线与所有线段相交
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8579 | Accepted: 2608 |
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
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! 题目大意:问是否存在一条直线所有线段在它上面的投影至少有一个公共交点,等同与这条直线的垂线与所有线段都有交点。即求是否有一条与所有线段相交。两两枚举线段四个端点两两成四条直线,
若所有的线段的两个端点分别在直线的两边(只要不是在同一边就行,在直线上也可以),那么说明存在这么一条直线。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std; struct Point{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
}; struct Segment{
Point a,b;
}; typedef Point Vector;
Vector operator -(const Point &A,const Point &B){ return Vector(A.x-B.x,A.y-B.y);}
bool operator < (const Point &a,const Point &b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
const double eps=1e-; int dcmp(double x)
{
if(fabs(x)<eps) return ;
else return x<?-:;
} bool operator == (const Point &a,const Point &b){
return (dcmp(a.x-b.x)== && dcmp(a.y-b.y)==);
}
double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}//点积
double Length(Vector A){return sqrt(Dot(A,A));}//向量长度
//两向量的夹角
double Angle(Vector A,Vector B){return acos(Dot(A,B)/Length(A)/Length(B));}
double Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}//叉积 vector<Segment> S; bool judge(Point a,Point b)
{
if(a == b) return false;//a,b属于同一个点,一个点不能确定一条直线
int i,n=S.size();
Vector v1=b-a,v2,v3;
for(i=;i<n;i++)
{
v2=S[i].a-a;
v3=S[i].b-a;
if(dcmp(Cross(v1,v2)*Cross(v1,v3)) > ) return false;
}
return true;
} bool solve()
{
int i,j,n=S.size();
for(i=;i<n;i++)
{
for(j=i+;j<n;j++)
if(judge(S[i].a,S[j].a) || judge(S[i].a,S[j].b) || judge(S[i].b,S[j].a) || judge(S[i].b,S[j].b))
return true;
}
return false;
}
int main()
{
int T,n,i;
Segment s;
scanf("%d",&T);
while(T--)
{
S.clear();
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%lf %lf %lf %lf",&s.a.x,&s.a.y,&s.b.x,&s.b.y);
S.push_back(s);
}
if(n==) printf("Yes!\n");
else if(solve()) printf("Yes!\n");
else printf("No!\n");
}
return ;
}
poj 3304 判断是否存在一条直线与所有线段相交的更多相关文章
- HDU 3492 (直线与所有线段相交) Segment
题意: 给出n个线段,判断是否存在一条直线使得所有线段在直线上的射影的交非空. 分析: 如果我们找到一条与所有线段相交的直线,然后做一条与该直线垂直的直线,这些线段在直线上的射影就一定包含这个垂足. ...
- POJ 3304 /// 判断线段与直线是否相交
题目大意: 询问给定n条线段 是否存在一条直线使得所有线段在直线上的投影存在公共点 这个问题可以转化为 是否存在一条直线与所有的线段同时相交 而枚举直线的问题 因为若存在符合要求的直线 那么必存在穿过 ...
- C - Segments POJ - 3304 (判断线段相交)
题目链接:https://vjudge.net/contest/276358#problem/C 题目大意:给你n条线段,问你是否存在一条线段使得所有的线段在这条直线的投影至少具有一个交点? 具体思路 ...
- poj 3304 找一条直线穿过所有线段
题目链接:http://poj.org/problem?id=3304 #include<cstdio> #include<cstring> #include<cmath ...
- Segments - POJ 3304 (判断直线与线段是否相交)
题目大意:给出一些线段,然后判断这些线段的投影是否有可能存在一个公共点. 分析:如果这些线段的投影存在一个公共点,那么过这个公共点作垂线一定与所有的直线都想交,于是题目转化成是否存在一个直线可以经 ...
- Intersecting Lines - POJ 1269(判断平面上两条直线的关系)
分析:有三种关系,共线,平行,还有相交,共线和平行都可以使用叉积来进行判断(其实和斜率一样),相交需要解方程....在纸上比划比划就出来了.... 代码如下: ================== ...
- 判断线段和直线相交 POJ 3304
// 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...
- 简单几何(线段与直线的位置) POJ 3304 Segments
题目传送门 题意:有若干线段,问是否存在一条直线,所有线段投影到直线上时至少有一个公共点 分析:有一个很好的解题报告:二维平面上线段与直线位置关系的判定.首先原问题可以转换为是否存在一条直线与所有线段 ...
- POJ 3304 Segments(直线)
题目: Description Given n segments in the two dimensional space, write a program, which determines if ...
随机推荐
- 删除.cpp文件
今天启动vc6.0后随手直接建了一个.cpp文件(没有建什么工程的),编译运行成功后,就把vc关了.后想把这个随手建的文件给删掉,却怎么也找不到这个文件,文件搜索或改变文件的属性也无法找到这个文件,即 ...
- Codeforces Round #317 (Div. 2) C Lengthening Sticks (组合,数学)
一个合法的三角形的充要条件是a<b+c,其中a为最长的一边,可以考虑找出所有不满足的情况然后用总方案减去不合法的情况. 对于一个给定的总长度tl(一定要分完,因为是枚举tl,不分配的长度已经考虑 ...
- Kafka-broker配置说明
配置文件在config/server.properties 下面的一些配置可能是你需要进行修改的. broker.id 整数,建议根据ip区分 log.dirs kafka存放消息文件的路径, 默认/ ...
- Vue 打印预览功能
需求有几种情况: 1.直接在HTML写页面,将页面上的东西用A4纸打印出来: 2.后台传回PDF文件,前台浏览器预览并打印: 3.后台做好要打印的,传回图片,如base64编码,前台浏览器 预览并打印 ...
- CPP-基础:wchar_t
目 录 1简介 2例如 3将char转换成wchar_t 1.简介 wchar_t是C/C++的字符数据类型,是一种扩展的字符存储方式,wchar_t类型主要用在国际化程序的实现中,但它不等同于uni ...
- 浅谈倍增LCA
题目链接:https://www.luogu.org/problemnew/show/P3379 刚学了LCA,写篇blog加强理解. LCA(Least Common Ancestors),即最近公 ...
- angular-file-upload 在IE下使用的坑
如果在控件配置里面设置了queueLimit属性为1,就是队列文件个数为1,并且在<input>标签设置里multiple属性. 在IE浏览器上传附件的时候,浏览器会报错“SCRIPT50 ...
- 牛客练习赛40 C-小A与欧拉路
求图中最短的欧拉路.题解:因为是一棵树,因此当从某一个节点遍历其子树的时候,如果还没有遍历完整个树,一定还需要再回到这个节点再去遍历其它子树,因此除了从起点到终点之间的路,其它路都被走了两次,而我们要 ...
- php-7.0.16 , apache2.4.25 配置
官网下载php,apache 修改apache E:\php\Apache24\conf\httpd.conf Define SRVROOT "E:/php/Apache24" - ...
- verilog behavioral modeling--blocking and nonblocking
BLOCKIN ...