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 ...
随机推荐
- JavaScript字符串的处理方法
1.字符方法charAt()和charCodeAt();这两个方法都接收一个参数 var stringValue = "hello world"; stringValue.char ...
- vue-router笔记
1.vue-router 安装 在安装webpack模块时就安装了 eg: vue init webpack demo (安装webpack模块并取名为demo) 在安装模块时没有安装的话 ...
- 20181030noip模拟赛T1
YY的矩阵 YY有一个大矩阵(N*M), 矩阵的每个格子里都有一个整数权值W[i,j](1<=i<=M,1<=j<=N) 对于这个矩阵YY会有P次询问,每次询问这个大矩阵的一个 ...
- C++笔记006:关于类的补充
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 关于类的补充: 类是一个数据类型(固定大小内存块的别名),定义一个类,是一个抽象的概念,不会给你分配内存,用数据类型定义变量的时候,才会分 ...
- Zabbix——部署(DB与web分离)
前提条件: 两台centos7设备 两台设备可以相互访问 Zabbix-Servser版本为4.0 mysql版本为8.0 关闭防火墙 Zabbix部署(包含server,web,agent) rpm ...
- Spring Boot在反序列化过程中:jackson.databind.exc.InvalidDefinitionException cannot deserialize from Object value
错误场景 用Spring boot写了一个简单的RESTful API,在测试POST请求的时候,request body是一个符合对应实体类要求的json串,post的时候报错. 先贴一段error ...
- mysql如何配置sql记录
原文链接:http://www.qqdeveloper.com/detail/11/1.html 为什么要记录sql记录 主要目的是为了检测我们的网站安全问题,有效的避免一些sql注入或者是xss攻击 ...
- Java三种代理模式
本文转自:https://mp.weixin.qq.com/s/nBmbNP2mR7ei-lDsuOxjWg 代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象 ...
- python教程(一)·命令行基本操作
先来了解下 "命令提示符". 等等?!既然本篇文章标题是"命令行基本操作",那怎么又说到"命令提示符"去了呢?客官莫要急,且听我说 命令提示 ...
- 计算阶乘的和v2.0(4分)
题目内容: 假设有这样一个三位数m,其百位.十位和个位数字分别是a.b.c,如果m= a!+b!+c!,则这个三位数就称为三位阶乘和数(约定0!=1).请编程计算并输出所有的三位阶乘和数. 函数原型: ...