POJ 3304 Segments (判断直线与线段相交)
题目链接:POJ 3304
Problem 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!
Solution
题意
给定 \(n\) 条线段,判断是否存在一条直线,使得所有线段在这条直线上的投影有公共部分。
题解
枚举 ToLeftTest
如果存在满足条件的一条直线,那么该直线的一条垂线一定与所有线段相交。
如果 \(n \le 2\) 时一定存在。
枚举所有线段的端点中任意两点构造的直线,判断是否所有线段都与之相交。
判断线段与直线相交的方法:判断线段的两个端点是否在直线的同一侧。
注意不要枚举两个相同的端点。
Code
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
typedef double db;
const db eps = 1e-10;
const db pi = acos(-1.0);
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 1e3 + 10;
inline int dcmp(db x) {
if(fabs(x) < eps) return 0;
return x > 0? 1: -1;
}
class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
void input() {
scanf("%lf%lf", &x, &y);
}
bool operator<(const Point &a) const {
return (!dcmp(x - a.x))? dcmp(y - a.y) < 0: x < a.x;
}
bool operator==(const Point &a) const {
return dcmp(x - a.x) == 0 && dcmp(y - a.y) == 0;
}
db dis2(const Point a) {
return pow(x - a.x, 2) + pow(y - a.y, 2);
}
db dis(const Point a) {
return sqrt(dis2(a));
}
db dis2() {
return x * x + y * y;
}
db dis() {
return sqrt(dis2());
}
Point operator+(const Point a) {
return Point(x + a.x, y + a.y);
}
Point operator-(const Point a) {
return Point(x - a.x, y - a.y);
}
Point operator*(double p) {
return Point(x * p, y * p);
}
Point operator/(double p) {
return Point(x / p, y / p);
}
db dot(const Point a) {
return x * a.x + y * a.y;
}
db cross(const Point a) {
return x * a.y - y * a.x;
}
};
typedef Point Vector;
class Line {
public:
Point s, e;
Line() {}
Line(Point s, Point e) : s(s), e(e) {}
void input() {
scanf("%lf%lf%lf%lf", &s.x, &s.y, &e.x, &e.y);
}
int toLeftTest(Point p) {
if((e - s).cross(p - s) > 0) return 1;
else if((e - s).cross(p - s) < 0) return -1;
return 0;
}
};
Line line[maxn];
Point point[maxn];
int main() {
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
line[i].input();
point[i * 2] = line[i].s;
point[i * 2 + 1] = line[i].e;
}
if(n < 3) {
printf("Yes!\n");
continue;
}
int fg = 0;
for(int i = 0; i < 2 * n; ++i) {
for(int j = i + 1; j < 2 * n; ++j) {
if(point[i] == point[j]) continue;
Line l = Line(point[i], point[j]);
int flag = 1;
for(int k = 0; k < n; ++k) {
if(l.toLeftTest(line[k].s) + l.toLeftTest(line[k].e) && l.toLeftTest(line[k].s) == l.toLeftTest(line[k].e)) {
flag = 0;
break;
}
}
if(flag == 1) {
printf("Yes!\n");
fg = 1;
break;
}
}
if(fg == 1) {break;}
}
if(!fg) {
printf("No!\n");
}
}
return 0;
}
POJ 3304 Segments (判断直线与线段相交)的更多相关文章
- POJ 3304 Segments 判断直线和线段相交
POJ 3304 Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以 ...
- POJ 3304 Segments (直线和线段相交判断)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7739 Accepted: 2316 Descript ...
- POJ 3304 Segments (直线与线段是否相交)
题目链接 题意 : 能否找出一条直线使得所有给定的线段在该直线上的投影有一个公共点. 思路 : 假设存在一条直线a使得所有线段在该直线上的投影有公共点,则必存在一条垂直于直线a的直线b,直线b与所有线 ...
- 判断直线与线段相交 POJ 3304 Segments
题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...
- POJ 1039 Pipe(直线和线段相交判断,求交点)
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8280 Accepted: 2483 Description ...
- poj3304(叉积判断直线和线段相交)
题目链接:https://vjudge.net/problem/POJ-3304 题意:求是否能找到一条直线,使得n条线段在该直线的投影有公共点. 思路: 如果存在这样的直线,那么在公共投影点作直线的 ...
- [poj] 3304 Segments || 判断线段相交
原题 给出n条线段,判断是否有一条直线与所有线段都有交点 若存在这样一条直线,那么一定存在一条至少过两个线段的端点的直线满足条件. 每次枚举两条线段的两个端点,确定一条直线,判断是否与其他线段都有交点 ...
- POJ 2074 /// 判断直线与线段相交 视野盲区
题目大意: 将所有物体抽象成一段横向的线段 给定房子的位置和人行道的位置 接下来给定n个障碍物的位置 位置信息为(x1,x2,y) 即x1-x2的线段 y相同因为是横向的 求最长的能看到整个房子的一段 ...
- POJ 3304 Segments(直线)
题目: Description Given n segments in the two dimensional space, write a program, which determines if ...
随机推荐
- spring 数据库字段映射
当有复杂名称字段时: 在repository中写代码字段名 List<Grid> findByLocIsWithin(GeoJsonPolygon boundary); 可以添加field ...
- JavaScript 获取时间,时间戳
一. 动态获取js时间 1.方法一:最简单的写法,直接输出时间到页面 <!DOCTYPE html> <html> <head> <title>< ...
- 改进持续交付中的CI环节
改进持续交付中的CI环节 在当前 DevOps 的趋势下,持续集成(CI)和持续部署(CD)具有支柱性地位,那么能够成功搭建 CI/CD 流水线就至关重要了. 今天我就讲一讲如何做好CI部分,让我们的 ...
- Java 实例 - 方法重载
先来看下方法重载(Overloading)的定义:如果有两个方法的方法名相同,但参数不一致,哪么可以说一个方法是另一个方法的重载. 具体说明如下: 方法名相同 方法的参数类型,个数顺序至少有一项不同 ...
- UVA1595_Symmetry
给出平面上n个点,问你能不能找到一个竖线让他们对称 这道题后面发现真的不难,又不止一种方法 我当时写的很挫,死脑筋的就找一个点的对称点存不存在,用结构体存点信息,在排序用find找,,然后不知道一堆w ...
- 现在就去100offer 参加互联网人才拍卖! 现在登录现在注册 为什么整个互联网行业都缺前端工程师?
现在,几乎整个互联网行业都缺前端工程师,不仅在刚起步的创业公司,上市公司乃至巨头,这个问题也一样存在.没错,优秀的前端工程师简直比大熊猫还稀少. 每天,100offer的HR群都有人在吐槽招不到前端工 ...
- spark性能调优02-JVM调优
1.降低cache操作的内存占比 1.1 为什么要降低cache操作的内存占比 spark的堆内存分别两部分,一部分用来给Rdd的缓存进行使用,另一部分供spark算子函数运行使用,存放函数中的对象 ...
- pthread_create()的一个错误示例
//pthread_create()函数的错误示例 //新建线程同时传入线程号.线程号总和和消息 #include <stdio.h> #include <pthread.h> ...
- element UI datepicker组件限制可选日期范围
长话短说,简单粗暴上代码了,在element中的datepicker,可以自由选择日期,如下: 然后我们根据element 官网的文档,给datepicker组件动态改变 picker-options ...
- web集群和分布式服务以及消息补偿机制几种方案
一.为什么要集群? 1.JavaEE项目,如果部署在一台Tomcat上,所有的请求,都由这一台服务器处理,存在很大风险: A:并发处理能力有限(一般单台服务器处理的并发量为250左右,超过250,可能 ...