Segments

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15335   Accepted: 4862

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 (x1y1) and (x2y2) 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

 
询问是否存在一条直线与所有线段相交
暴力线段的断点构成直线,判断是否可行。
 //2017-08-30
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int N = ;
const double EPS = 1e-; struct Point{
double x, y;
Point(){}
Point(double _x, double _y):x(_x), y(_y){}
Point(const Point &p):x(p.x), y(p.y){}
//a-b为向量ba
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;
}
}; struct Line{
Point a, b;
Line(){}
Line(Point _a, Point _b):a(_a), b(_b){}
void set_a_b(const Point &_a, const Point &_b){
a = _a;
b = _b;
}
}seg[N]; //三态函数
int sgn(double x){
if(fabs(x) < EPS)return ;
if(x < )return -;
else return ;
} //input:Point a;Point b
//output:distance a、b两点间的距离
//note:该函数取名distance会编译错误
double dist(Point a, Point b){
return sqrt((a-b)*(a-b));
} //input:l1 直线l1;l2 线段l2
//output:true 直线l1与线段l2相交;false 直线l1与线段l2不想交
bool seg_inter_line(Line l1, Line l2){
return sgn((l2.a-l1.b)^(l1.a-l1.b))*sgn((l2.b-l1.b)^(l1.a-l1.b)) <= ;
} int n;
//input:直线line
//output:true 直线与所有线段都相交
bool all_inter(Line line){
for(int i = ; i < n; i++)
if(!seg_inter_line(line, seg[i]))
return false;
return true;
} bool check(){
Line line;
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(i == j)continue;
if(dist(seg[i].a, seg[j].a) > EPS){
line.set_a_b(seg[i].a, seg[j].a);
if(all_inter(line))return true;
}
if(dist(seg[i].a, seg[j].b) > EPS){
line.set_a_b(seg[i].a, seg[j].b);
if(all_inter(line))return true;
}
if(dist(seg[i].b, seg[j].a) > EPS){
line.set_a_b(seg[i].b, seg[j].a);
if(all_inter(line))return true;
}
if(dist(seg[i].b, seg[j].b) > EPS){
line.set_a_b(seg[i].b, seg[j].b);
if(all_inter(line))return true;
}
}
}
return false;
} int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputC.txt", "r", stdin);
int T;
cin>>T;
while(T--){
cin>>n;
for(int i = ; i < n; i++){
cin>>seg[i].a.x>>seg[i].a.y>>seg[i].b.x>>seg[i].b.y;
}
if(check() || n == || n == )cout<<"Yes!"<<endl;
else cout<<"No!"<<endl;
} return ;
}

POJ3304(KB13-C 计算几何)的更多相关文章

  1. poj3304 Segments【计算几何】

    C - Segments POJ - 3304 最近开始刷计算几何了 公式好多完全不会 数学不行 几何不行 记忆力不行 当机 查的题解 就当复习吧 这套专题拿来熟悉一下计算几何模板 #include ...

  2. poj3304计算几何直线与线段关系

    Given n segments in the two dimensional space, write a program, which determines if there exists a l ...

  3. 计算几何——线段和直线判交点poj3304

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #i ...

  4. ACM/ICPC 之 计算几何入门-叉积-to left test(POJ2318-POJ2398)

    POJ2318 本题需要运用to left test不断判断点处于哪个分区,并统计分区的点个数(保证点不在边界和界外),用来做叉积入门题很合适 //计算几何-叉积入门题 //Time:157Ms Me ...

  5. HDU 2202 计算几何

    最大三角形 Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. ACM 计算几何中的精度问题(转)

    http://www.cnblogs.com/acsmile/archive/2011/05/09/2040918.html 计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模 ...

  7. hdu 2393:Higher Math(计算几何,水题)

    Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)

    Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a b ...

  9. [知识点]计算几何I——基础知识与多边形面积

    // 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vxaq.html 1.前言 ...

随机推荐

  1. Python系列之——利用Python实现微博监控

    0x00 前言: 前几个星期在写一个微博监控系统 可谓是一波三折啊 获取到微博后因为一些字符编码问题 导致心态爆炸开发中断 但是就在昨天发现了另外一个微博的接口 一个手机微博的接口https://m. ...

  2. per学习笔记-zkclient,curator使用

    开源客户端,原生api的不足 连接的创建是异步的,需要开发人员自行编码实现等待 连接没有自动的超时重连机制 Zk本身没提供序列化机制,需要开发人员自行指定,从而实现数据的序列化和反序列化 Watche ...

  3. golang 切片和数组在for...range中的区别

    切片是引用类型,而数组是值类型,并且for...range有以下规则: range表达式只会在for语句开始执行时被求值一次,无论后边会有多少次迭代 range表达式的求值结果会被复制,也就是说,被迭 ...

  4. 使用pymysql

    安装 pip3 install pymysql 连接.执行sql.关闭(游标) import pymysql mysql_connect_dict={ 'host':'127.0.0.1', 'por ...

  5. 副本集mongodb 无缘无故 cpu异常

    mondb 服务器故障 主从复制集 主:   192.168.1.106从:   192.168.1.100仲裁:192.168.1.102 os版本:CentOS Linux release 7.3 ...

  6. 原生js,jquery ajax请求以及jsonp的调用

    ajax    是用来处理前后端交互的技术,可以改善用户体验,其本质是    XMLHttpRequest,异步访问服务器并发送请求数据,服务器返回响应的数据,以页面无刷新的效果改变页面中的局部内容  ...

  7. Yii2+Swagger搭建RESTful风格的API项目

    在现有的Advanced Template上搭建RESTful API项目的步骤: 本案例前提说明: 本例中不使用\yii\rest\ActiveController自动创建的API,而是自定义一个A ...

  8. rabbitmq系列五 之主题交换机

    1.主题 在前面的例子中,我们对日志系统进行了改进.使用了direct交换机代替了fanout交换机,从只能盲目的广播消息改进为有可能选择性的接收日志. 尽管直接交换机能够改善我们的日志系统,但是它也 ...

  9. One difference between AngularJS' $location and window.location

    Recenently, I encountered a problem. Client side code is: $http({ url: "/api/runtimelicense&quo ...

  10. 课程一(Neural Networks and Deep Learning),第四周(Deep Neural Networks) —— 3.Programming Assignments: Deep Neural Network - Application

    Deep Neural Network - Application Congratulations! Welcome to the fourth programming exercise of the ...