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.
题目大意:给n条线段,问是否存在一条直线穿过所有线段
思路:题目本身没什么难度,就是枚举所有穿过线段端点的直线即可,难是难在精度的控制。第一个要注意的地方是n=1的时候要特判,第二个要注意的是如果两点过于接近是不能作为枚举直线的,因为这样算出来的叉积过小,很容易小于EPS。注意这两点就没什么大问题了。
#include <cstdio>
#include <cmath> #define EPS 1e-10
#define Abs(x) x>0?x:-x typedef double TYPE;
//2D point
struct Point {
TYPE x, y;
Point() {}
Point(double xx, double yy): x(xx), y(yy) {}; bool operator < (const Point &B) const {
if(x == B.x) return y < B.y;
else return x < B.x;
}
};
//if return>0 then a is on the clockwise of b
TYPE cross(const Point &a, const Point &b, const Point &o) {
return (o.x - a.x) * (o.y - b.y) - (o.x - b.x) * (o.y - a.y);
}
//distance between a and b
inline double dist(Point a, Point b){
double x = a.x - b.x, y = a.y - b.y;
return sqrt(x * x + y * y);
}
//2D twp-point form segment
struct Seg {
Point st, ed;
Seg() {}
Seg(Point aa, Point bb):st(aa), ed(bb) {}
};
//if a straddle b
inline bool straddle(const Seg &A, const Seg &B) {
return cross(B.st, A.st, B.ed) * cross(B.st, A.ed, B.ed) < EPS;
} #define MAXN 110 int T, n;
Seg sg[MAXN]; inline bool check(Seg p) {
if(dist(p.st, p.ed) < EPS) return false;
for(int i = 0; i < n; ++i)
if(!straddle(sg[i], p)) return false;
return true;
} bool solve() {
for(int i = 0; i < n; ++i) for(int j = i+1; j < n; ++j) {
if(check(Seg(sg[i].st, sg[j].st))) return true;
if(check(Seg(sg[i].st, sg[j].ed))) return true;
if(check(Seg(sg[i].ed, sg[j].st))) return true;
if(check(Seg(sg[i].ed, sg[j].ed))) return true;
}
return false;
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 0; i < n; ++i) scanf("%lf%lf%lf%lf", &sg[i].st.x, &sg[i].st.y, &sg[i].ed.x, &sg[i].ed.y);
if(n == 1 || solve()) puts("Yes!");
else puts("No!");
}
}
POJ 3304 Segments(线的相交判断)的更多相关文章
- POJ 3304 Segments 基础线段交判断
LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...
- POJ 3304 Segments (叉乘判断线段相交)
<题目链接> 题目大意: 给出一些线段,判断是存在直线,使得该直线能够经过所有的线段.. 解题思路: 如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为 ...
- 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(计算几何:直线与线段相交)
POJ 3304 Segments 大意:给你一些线段,找出一条直线可以穿过全部的线段,相交包含端点. 思路:遍历全部的端点,取两个点形成直线,推断直线是否与全部线段相交,假设存在这种直线,输出Yes ...
- POJ 3304 Segments (判断直线与线段相交)
题目链接:POJ 3304 Problem Description Given n segments in the two dimensional space, write a program, wh ...
- poj 3304线段与直线相交
http://poj.org/problem?id=3304 Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- poj 3304 Segments(解题报告)
收获:举一反三:刷一道会一道 1:思路转化:(看的kuangbin的思路) 首先是在二维平面中:如果有很多线段能够映射到这个直线上并且至少重合于一点,充要条件: 是过这个点的此条直线的垂线与其他所有直 ...
- POJ 3304 Segments (直线和线段相交判断)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7739 Accepted: 2316 Descript ...
随机推荐
- 【Django笔记三】Django2.0配置mysql模型
一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 Mysql版本: 5.5.53 安装mysql 二.安装Mysqlclient: 1. ...
- anyconnect connection attempt has failed
anyconnect connection attempt has failed 在控制面板-网络与Internet-网络连接,右键AnyConnect secure连接适配器,点击属性 在连接项目中 ...
- 大数据学习--day04(选择结构、循环结构、大数据java基础面试题)
选择结构.循环结构.大数据java基础面试题 switch: 注意: byte short int char String(jdk1.7支持) 不能是 long float double boolea ...
- Shuffling Machine
7-43 Shuffling Machine(20 分) Shuffling is a procedure used to randomize a deck of playing cards. Bec ...
- MongoDB入门---聚合操作&管道操作符&索引的使用
经过前段时间的学习呢,我们对MongoDB有了一个大概的了解,接下来就要开始使用稍稍深入一点的东西了,首先呢,就是MongoDB中的聚合函数,跟mysql中的count等函数差不多.话不多说哈,我们先 ...
- war2 洛谷模拟赛day2 t3 状压
(new ) war2 题解:总体数据而言,我们很容易想到着就是DP啊,我们DP数组,用状态压缩,代表有那些点已经被占领过了,代表上一次我占的是那个.对于每一次状态转移,若当前我们要占领的Port ...
- spark2.2 从入门到精通 视频教程 百度云网盘下载地址
spark2.2 从入门到精通 视频教程 百度云网盘下载地址 链接:https://pan.baidu.com/s/1sm2Jdmt 密码:rdea
- 菜鸟学Linux - 变量基本规则
变量是一个很重要的概念,无论是bash脚本还是其他语言,都是如此.在bash中,创建变量很简单,给变量一个名称即可.默认情况下,变量的值为空.我们可以通过等号为变量赋值.需要注意的是,变量和变量的值不 ...
- Android Parcelable 源码解析
大家都知道,要想在Intent里面传递一些非基本类型的数据,有两种方式,一种实现Parcelable,另一种是实现Serializable接口. 今天先不说Serializable 接口,只说Parc ...
- springboot 读写excel
添加两个坐标: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...