POJ3304(KB13-C 计算几何)
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 (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
//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 计算几何)的更多相关文章
- poj3304 Segments【计算几何】
C - Segments POJ - 3304 最近开始刷计算几何了 公式好多完全不会 数学不行 几何不行 记忆力不行 当机 查的题解 就当复习吧 这套专题拿来熟悉一下计算几何模板 #include ...
- poj3304计算几何直线与线段关系
Given n segments in the two dimensional space, write a program, which determines if there exists a l ...
- 计算几何——线段和直线判交点poj3304
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #i ...
- ACM/ICPC 之 计算几何入门-叉积-to left test(POJ2318-POJ2398)
POJ2318 本题需要运用to left test不断判断点处于哪个分区,并统计分区的点个数(保证点不在边界和界外),用来做叉积入门题很合适 //计算几何-叉积入门题 //Time:157Ms Me ...
- HDU 2202 计算几何
最大三角形 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- ACM 计算几何中的精度问题(转)
http://www.cnblogs.com/acsmile/archive/2011/05/09/2040918.html 计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模 ...
- hdu 2393:Higher Math(计算几何,水题)
Higher Math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
- [知识点]计算几何I——基础知识与多边形面积
// 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vxaq.html 1.前言 ...
随机推荐
- Django 实现第三方账号登录网站
这里我们使用 django-allauth 模块来实现第三方账号验证登录,官方文档如下:https://django-allauth.readthedocs.io/en/latest/ . 安装 dj ...
- python收集jvm数据
之前前辈用 java 写的收集 jvm 脚本, 不太方便组内小伙伴维护, 遂用 python 重写了 #!/usr/bin/env python # -*- coding: utf-8 -*- # F ...
- MySQL 逻辑物理备份测试
目录 逻辑备份 mysqldump 普通备份 mysqlpump 并行备份 mysqlpump 压缩并行备份 mydumper 并行备份 mydumper 并行压缩备份 小结 物理备份 xtrabac ...
- java批量读取多个文件并存入数据库
有时候服务运行的日志文件,需要统计分析,但数据量很大,并且直接在文件中看很不直观,这时可以将文件中的内容导入到数据库,入库后的数据就可以按照需求进行统计分析了. 这个是以服务器的访问日志作为示例,一个 ...
- iOS开发笔记-图标和图片大小官方最新标准
这两天开发iOS app用到了Tab bar,然后随便切了点图标放上去发现效果极差.于是乎,开始查找苹果官方给的标准.搜索一番后,看到了一篇博文,但其内容与iOS人机交互指南最新版内容不符. 故此,在 ...
- 10-05 Java 内部类概述和讲解
内部类的概述 /* 内部类概述: 把类定义在其他类的内部,这个类就被称为内部类. 举例:在类A中定义了一个类B,类B就是内部类. 内部的访问特点: A:内部类可以直接访问外部类的成员,包括私有. B: ...
- 10 Tips for Optimizing Your Website’s Speed
转自:http://sixrevisions.com/web-development/site-speed-performance/ Web page speed and performance is ...
- 输入两棵二叉树A,B,判断B是不是A的子结构(c++实现)
#include <iostream> #include <cstdio> #include <stdio.h> #include <string> # ...
- php -- 数据库信息
----- 023-dbinfo.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...
- JavaScript -- Window-Move,Print
-----035-Window-Move.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=& ...