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 ...
随机推荐
- LeetCode 中级 - 组合总和(105)
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
- jquery输入框动态查询l<li></li>列表
1.html代码如下: 2.js代码如下: $('#search').bind('input propertychange', function () { searchKpoint(); }); fu ...
- 【saltstack 集中化管理】
Master(监控端): Minion(被监控端) 监控: /etc/master: #interface:监控端地址 #自动接受被监控端证书 #saltstack文件根目录位置 #启动监控 被监控: ...
- hadoop生态搭建(3节点)-17.sqoop配置_单节点
# ==================================================================安装 sqoop tar -zxvf ~/sqoop-1.4.7 ...
- spark启动原理总结
一般情况下,我们启动spark集群都是start-all.sh或者是先启动master(start-master.sh),然后在启动slave节点(start-slaves.sh),其实翻看start ...
- SaltStack error: No module named 'salt'
启动saltstack的时候出现下面的错误 问题原因 是因为我在centos7中安装了多版本的python导致的 解决方案 将文件下面文件首行更改成python2 [root@saltstack-12 ...
- telnet 批处理
**** 需要确认多台服务器端口是否打开,如果一个一个telnet会非常麻烦,通过百度,写了两个BAT,基本能做工作需要. ***start.bat start "" " ...
- nodejs module/require
1. wrap up a module using module.exports, name the file to a.js var fun1=function(){ var stylus = re ...
- COGS:1822. [AHOI2013]作业
1822. [AHOI 2013] 作业 ★★★ 输入文件:ahoi2013_homework.in 输出文件:ahoi2013_homework.out 简单对比时间限制:20 s ...
- spring-boot日志操作
SpringBoot Logback日志配置 Logback的配置介绍: 1.Logger.appender及layout Logger作为日志的记录器,把它关联到应用的对应的context上后,主要 ...