POJ 3304 Segments(判断直线与线段是否相交)
题目传送门: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 (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!
题目大意:
在一个平面上,给你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(判断直线与线段是否相交)的更多相关文章
- POJ 3304 Segments 判断直线和线段相交
POJ 3304 Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以 ...
- POJ 3304 Segments (判断直线与线段相交)
题目链接:POJ 3304 Problem Description Given n segments in the two dimensional space, write a program, wh ...
- POJ 3304 Segments (直线与线段是否相交)
题目链接 题意 : 能否找出一条直线使得所有给定的线段在该直线上的投影有一个公共点. 思路 : 假设存在一条直线a使得所有线段在该直线上的投影有公共点,则必存在一条垂直于直线a的直线b,直线b与所有线 ...
- POJ 3304 Segments (直线和线段相交判断)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7739 Accepted: 2316 Descript ...
- [poj] 3304 Segments || 判断线段相交
原题 给出n条线段,判断是否有一条直线与所有线段都有交点 若存在这样一条直线,那么一定存在一条至少过两个线段的端点的直线满足条件. 每次枚举两条线段的两个端点,确定一条直线,判断是否与其他线段都有交点 ...
- Segments - POJ 3304 (判断直线与线段是否相交)
题目大意:给出一些线段,然后判断这些线段的投影是否有可能存在一个公共点. 分析:如果这些线段的投影存在一个公共点,那么过这个公共点作垂线一定与所有的直线都想交,于是题目转化成是否存在一个直线可以经 ...
- POJ 3304 Segments(直线)
题目: Description Given n segments in the two dimensional space, write a program, which determines if ...
- 简单几何(线段与直线的位置) POJ 3304 Segments
题目传送门 题意:有若干线段,问是否存在一条直线,所有线段投影到直线上时至少有一个公共点 分析:有一个很好的解题报告:二维平面上线段与直线位置关系的判定.首先原问题可以转换为是否存在一条直线与所有线段 ...
- 判断直线与线段相交 POJ 3304 Segments
题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...
随机推荐
- C# 星期相关代码实例
本文为引用文章 仅作整理自用 原文链接: https://www.cnblogs.com/yxyl/p/9992841.html @网吧看压力大 从周一到周日的顺序,获取排序数值: int i = D ...
- H3C 广域网连接方式
- 微信接口开发报错invalid credential, access_token is invalid or not latest hint
微信接口凭证access_token一定要全局管理 我们的查酒后台集成了微信公众平台的客服API接口,不用登录微信公众号的后台就可以直接给用户发送消息.最近,运营的同事反馈,通过微信查酒,后台无法直接 ...
- H3C 对等通信
- Python--day62--什么时候用GET请求和POST请求
1,GET请求和POST请求 都数据HTTP协议规定的请求方法 2,什么时候用GET请求? 1,浏览器想要得到一个HTML页面的时候 2,搜索引擎查询关键字的时候 3,什么时候用POST? 1,像后端 ...
- hibernate无限递归问题
项目异常如下: 2018-01-26 17:12:38.162 WARN 3128 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionReso ...
- 前端开发之BOM和DOM
BOM BOM:是指浏览器对象模型,它使JavaScript可以和浏览器进行交互. 1,navigator对象:浏览器对象,通过这个对象可以判定用户所使用的浏览器,包含了浏览器相关信息. naviga ...
- Codeforces Round #178 (Div. 2)
A. Shaass and Oskols 模拟. B. Shaass and Bookshelf 二分厚度. 对于厚度相同的书本,宽度竖着放显然更优. 宽度只有两种,所以枚举其中一种的个数,另一种的个 ...
- java 利用反射创建对象
创建对象: 1.使用Class对象的newInstance()方法创建该Class对象的实例,此时该Class对象必须要有无参数的构造方法. 2.使用Class对象获取指定的Constructor对象 ...
- 【js】React-Native 初始化时报错
一.按照官网的步骤一步一步的操作,到最后 react-native init AwesomeProject 时就是报错 报错信息如下图 然后我下载了这个模块 npm install prompt ...