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> ...
随机推荐
- [国嵌攻略][103][Linux内核模块基础]
什么是内核模块 Linux内核的整体结构非常庞大,其中包含的组件也非常多,如何使用这些组件.一种方式是把所有的组件都编译进内核文件,即zImage或bzImage,但这样会导致一个问题,占用内存过多. ...
- [国嵌笔记][027][ARM协处理器访问指令]
协处理器作用 协处理器用于执行特定的处理任务,如数学协处理器可以执行控制数字处理,以减轻处理器的负担.ARM处理器最多可以支持16个协处理器,其中CP15是最重要的一个协处理器 CP15的作用 CP1 ...
- POJ 3278 Catch That Cow(模板——BFS)
题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...
- parsing XML document from class path resource [config/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [config/applicationContext.xml] 解决方案
parsing XML document from class path resource [config/applicationContext.xml]; nested exception is j ...
- 98、vue.js简单入门
本篇导航: 介绍与安装 vue常用指令 一.介绍与安装 vue是一套构建用户界面的JAVASCRIPT框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层, ...
- 邓_ Jqery·笔记本【照片】
-------------------------------------------------------------------------------------------- [PHP] - ...
- @ property 与@ synthesize 的作用 VS @interface
表示声明了一个实例属性和它的getter和setter器 只在@interface中定义变量的话,你所定义的变量只能在当前的类中访问,在其他类中是访问不了的:而用@property声明的变量可以在外部 ...
- HTML <a href >标签的target属性
规定在何处打开链接文档. _blank 在新窗口中打开被链接文档 _parent 在父框架集中打开被链接文档 _self 默认.在相同的框架中打开被链接文档 _top 在整 ...
- nodejs+websocket实时聊天系统
介绍下websocket: webSocket协议本质上是一个基于tcp的协议; 建立一个websocket连接,大体的过程: 1.客户端浏览器首先向服务器发起一个http请求,这个请求和平常的请求有 ...
- vue中组件之间的相互调用,及通用后台管理系统左侧菜单树的迭代生成
由于本人近期开始学习使用vue搭建一个后端管理系统的前端项目,在左侧生成菜单树的时候遇到了一些问题.在这里记录下 分析:由于本人设定的菜单可以使多级结构,直接使用vue的v-for 遍历并不是很方便. ...