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 ...
随机推荐
- PHP的Session机制解析 1
一.php的默认session机制 php默认用磁盘文件来实现session在php.ini中session.save_handler = files定义session机制session.save_p ...
- AutoFac控制反转 转载https://blog.csdn.net/u011301348/article/details/82256791
一.AutoFac介绍 Autofac是.NET里IOC(Inversion of Control,控制反转)容器的一种,同类的框架还有Spring.NET,Unity,Castle等.可以通过NuG ...
- jenkins中的pipeline学习
Jenkins pipeline 升级打怪攻略(二): http://haurqb42j0gnc7zyxam.exp.bcevod.com/mda-jg9eq66184z5manh/mda-jg9eq ...
- MySql常见的错误
一些MySql运行中遇到的错误总结,大家也可以留言进行我进行汇总. 一.Unknown error 1146 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxEr ...
- 面向对象&从这里开始我们将不再是纯小白
一.面向对象初识 1.1 面向过程编程vs函数式编程 我们在没有学习函数的时候,写的代码都是面向过程式编程 # 面向过程编程 测量对象的元素的个数. s1 = 'fjdsklafsjda' count ...
- 学习vim 从常用按键开始
ctrl+e 将屏幕下滚一行 ctrl+u 将屏幕上滚半页 ctrl+d 将屏幕下滚半页 ctrl+b 将屏幕上滚一页 ctrl+f 将屏幕下滚一页 撤销 u 前进 ctrl r 移动 下一个单词 ...
- go strconv
strconv是golang用来做数据类型转换的一个库. 介绍下strconv最常用的两个方法, 没有解释语言那么自在可以str(int),int(string), 那还算简练. num, err : ...
- linux执行wget url时提示“无法建立 SSL 连接”
linux执行wget url时提示“无法建立 SSL 连接” 原因: wget在使用HTTPS协议时,默认会去验证网站的证书,而这个证书验证经常会失败 解决方案: 1.加上参数“--no-check ...
- RPC协议的介绍
根据网上搜索的一些资料摘抄汇总的,如果有误,欢迎斧正. 早期单机时代,一台电脑上运行多个进程,大家各干各的,老死不相往来.假如A进程需要一个画图的功能,B进程也需要一个画图的功能,程序员就必须为两个进 ...
- 【转载】vue-cli搭建的环境,用nginx做代理服务器,访问时显示:Invalid Host header
来源:https://blog.csdn.net/Cookysurongbin/article/details/86077241 vue-cli搭建的环境,用nginx做代理服务器,访问时显示:Inv ...