POJ 3304 Segments[直线与线段相交]
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 13514 | Accepted: 4331 |
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!
Source
题目大意:给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!。
暴力枚举线段的交点组成直线然后判相交就行了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=;
const double eps=1e-;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
inline int sgn(double x){
if(abs(x)<eps) return ;
else return x<?-:;
}
struct Vector{
double x,y;
Vector(double a=,double b=):x(a),y(b){}
bool operator <(const Vector &a)const{
return x<a.x||(x==a.x&&y<a.y);
}
void print(){
printf("%lf %lf\n",x,y);
}
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==&&sgn(a.y-b.y)==;} double Cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
double DisPP(Point a,Point b){
Point t=a-b;
return sqrt(t.x*t.x+t.y*t.y);
}
struct Line{
Point s,t;
Line(){}
Line(Point p,Point v):s(p),t(v){}
}a[N];
bool isLSI(Line l1,Line l2){
//puts("isLSI");
//l1.s.print();l1.t.print();
//l2.s.print();l2.t.print();
Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s;
//printf("%d %d end\n",sgn(Cross(v,u)),sgn(Cross(v,w)));
return sgn(Cross(v,u))!=sgn(Cross(v,w))||!sgn(Cross(v,u));
}
int n,m;
double x,y,x2,y2;
bool check(Line l){
if(sgn(DisPP(l.s,l.t))==) return false;
//printf("check\n");
//l.s.print();l.t.print();
for(int i=;i<=n;i++)
if(!isLSI(l,a[i])) return false;
return true;
} void solve(){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(check(Line(a[i].s,a[j].s))||check(Line(a[i].s,a[j].t))
||check(Line(a[i].t,a[j].s))||check(Line(a[i].t,a[j].t)))
{puts("Yes!");return;}
puts("No!");
} int main(int argc, const char * argv[]) {
int T=read();
while(T--){
n=read();
for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2);
a[i]=Line(Point(x,y),Point(x2,y2));
}
solve();
} return ;
}
POJ 3304 Segments[直线与线段相交]的更多相关文章
- Segments - POJ 3304 (判断直线与线段是否相交)
题目大意:给出一些线段,然后判断这些线段的投影是否有可能存在一个公共点. 分析:如果这些线段的投影存在一个公共点,那么过这个公共点作垂线一定与所有的直线都想交,于是题目转化成是否存在一个直线可以经 ...
- POJ 3304 Segments(线的相交判断)
Description Given n segments in the two dimensional space, write a program, which determines if ther ...
- POJ 2074 /// 判断直线与线段相交 视野盲区
题目大意: 将所有物体抽象成一段横向的线段 给定房子的位置和人行道的位置 接下来给定n个障碍物的位置 位置信息为(x1,x2,y) 即x1-x2的线段 y相同因为是横向的 求最长的能看到整个房子的一段 ...
- POJ 3304 Segments(计算几何:直线与线段相交)
POJ 3304 Segments 大意:给你一些线段,找出一条直线可以穿过全部的线段,相交包含端点. 思路:遍历全部的端点,取两个点形成直线,推断直线是否与全部线段相交,假设存在这种直线,输出Yes ...
- POJ 3304 Segments 判断直线和线段相交
POJ 3304 Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以 ...
- POJ 3304 Segments(判断直线与线段是否相交)
题目传送门:POJ 3304 Segments Description Given n segments in the two dimensional space, write a program, ...
- 判断直线与线段相交 POJ 3304 Segments
题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...
- poj 3304(直线与线段相交)
传送门:Segments 题意:线段在一个直线上的摄影相交 求求是否存在一条直线,使所有线段到这条直线的投影至少有一个交点 分析:可以在共同投影处作原直线的垂线,则该垂线与所有线段都相交<==& ...
- poj 3304 Segments (题意理解出错,错误的只枚举了过线段的直线)
//枚举过每一条线段的直线, //再判断其他线段的点在直线上或被直线穿过 //即求直线与线段相交(叉积) #include<stdio.h> #include<math.h> ...
随机推荐
- c++(挑选最大的n个数)
从一堆数据中挑选n个最大的数,这个问题是网上流传的比较广的几个问题之一.具体来说,它的意思就是:假设我们有100个数据,我们需要挑选出最大的n个数据(n < 100),那么有没有办法实现这样一个 ...
- 安装配置最强Spark IDE--IDEA
1.安装IntelliJ IDEA IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示 ...
- Spider_Man_4 の BeautifulSoup
一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...
- JavaScript实现职责链模式
什么是职责链模式 职责链模式的定义是:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止.举个例子:当你从公 ...
- POJ 1502 MPI Maelstrom(模板题——Floyd算法)
题目: BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distri ...
- linux CentOS部署【minimal 】
1.为什么选择CentOS不选择其他版本:http://www.cnblogs.com/TeemoHQ/p/6377260.html 2.准备的资源:VMware[官网下载],CentOS镜像 [阿里 ...
- thinkphp 中concat(连接)使用方法
1.concat将title和id连接作为truename新的字段,left从url字段左侧开始截取25个字符,同理right也可. 2.getLastql用法:
- arclistsg文档独立模型标签
[标签名称] arclistsg [标签简介] 单表独立模型的文档列表调用标记 [功能说明] 用于调用单表模型的内容,在V5.3系统以上版本中加入了单表模型的概念,脱离了以前的主从表的数据表关联结构, ...
- 将DedeCMS从子目录移动到根目录的方法
http://www.commonie.com/a/chat/dedeskill/298.html 以前做了一个Wordpress的博客,后来觉得采用DedeCMS更好一点,所以就有了转向DedeCM ...
- [SinGuLaRiTy] 复习模板-数学
[SinGuLaRiTy-1047] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 质因数分解 void solve(int n) { == ...