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.
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!
题意:给出n条线段 判断是否存在一条直线 使所有线段在这条直线上的投影都有至少一个公共点
思路:经过一些奇妙的转变 可以将题目转换为从所有线段中任选两个端点组成的直线是否可以穿过所有的线段 需要对选取的两个端点进行去重
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=;
const double eps=1e-;
int t,n;
double x,y,xx,yy; int dcmp(double x){
if(fabs(x)<eps) return ;
if(x<) return -;
return ;
} struct Point{
double x,y;
Point(){}
Point(double _x,double _y){
x=_x,y=_y;
}
Point operator + (const Point &b) const {
return Point(x+b.x,y+b.y);
}
Point operator - (const Point &b) const {
return Point(x-b.x,y-b.y);
}
double operator * (const Point &b) const {
return x*b.x+y*b.y;
}
double operator ^ (const Point &b) const {
return x*b.y-y*b.x;
}
}; struct Line{
Point s,e;
Line(){}
Line(Point _s,Point _e){
s=_s,e=_e;
}
}line[maxn]; double xmult(Point p0,Point p1,Point p2){
return (p1-p0)^(p2-p0);
} bool Seg_inter_line(Line l1,Line l2){
return dcmp(xmult(l2.s,l1.s,l1.e))*dcmp(xmult(l2.e,l1.s,l1.e))<=;
} double dist(Point a,Point b){
return sqrt((b-a)*(b-a));
} bool check(Line l1,int n){
if(dcmp(dist(l1.s,l1.e))==) return false; //判断重复点
for(int i=;i<n;i++)
if(Seg_inter_line(l1,line[i])==false)
return false;
return true;
} int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy);
line[i]=Line(Point(x,y),Point(xx,yy));
}
bool flag=false;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
if(check(Line(line[i].s,line[j].s),n) || check(Line(line[i].e,line[j].e),n) || check(Line(line[i].s,line[j].e),n) || check(Line(line[i].e,line[j].s),n)){
flag=true;
break;
}
if(flag) printf("Yes!\n");
else printf("No!\n");
}
return ;
}
POJ 3304 Segments(直线)的更多相关文章
- POJ 3304 Segments[直线与线段相交]
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13514 Accepted: 4331 Descrip ...
- POJ 3304 Segments(计算几何:直线与线段相交)
POJ 3304 Segments 大意:给你一些线段,找出一条直线可以穿过全部的线段,相交包含端点. 思路:遍历全部的端点,取两个点形成直线,推断直线是否与全部线段相交,假设存在这种直线,输出Yes ...
- 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 Problem Description Given n segments in the two dimensional space, write a program, wh ...
- POJ 3304 Segments 基础线段交判断
LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...
- POJ 3304 Segments (直线和线段相交判断)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7739 Accepted: 2316 Descript ...
- poj 3304 Segments 线段与直线相交
Segments Time Limit: 1000MS Memory Limit: 65536K Description Given n segments in the two dim ...
- poj 3304 Segments(计算直线与线段之间的关系)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10921 Accepted: 3422 Descrip ...
随机推荐
- C# GDI+绘制一维条码打印模糊的解决办法
最近遇到使用zxing生成的一维条码打印出来的条码图形很模糊根本识别不了.其实原因只有一句话: bitmap没有直接使用PrintDocument的Graphics画布进行绘制,而是中间处理了一下外部 ...
- C# 远程获取图片二进制
直接上代码, 紧做记录. public byte[] GetByteByImgUrl() { System.Net.WebRequest webreq = System.Net.WebRequest. ...
- iOS 简易型标签的实现(UICollectionView)
https://blog.csdn.net/sinat_39362502/article/details/80900984 2018年07月03日 16:49:05 Recorder_MZou 阅读数 ...
- mybatis 使用注解简化xml映射文件
目录 关于mybatis注解 初次简单使用mybatis注解示例 利用注解实现指定映射 使用注解实现表间关联(1对1) 关于mybatis注解 注解在java中特别常见,mybatis中也支持注解. ...
- JQuery 选择某个td中第二个a标签 控制特殊样式
a标签没有disabled属性,那么当我们想禁用a标签的点击事件的时候按照下面方法设置. 下面是html代码: <a id="entry” class="entry" ...
- 父元素高度设置为min-height,子元素高度设置为100%,但实际上子元素高度你知道是多少吗?
前言 给父元素一个min-height,子元素设置height:100%. 代码 <!DOCTYPE html> <html> <head> <title&g ...
- python中的编码问题
遇到的问题: (1)ValueError: source code string cannot contain null bytes 发现文件的编码被改成了UTF-16BE,使用pycharm设置项目 ...
- AirBnB春招笔试题
试题说明 笔试题只有一道,限时1小时. 模拟一个战争外交游戏,游戏中定义了三种操作: A city1 Hold : 军队A 占领了city1 A city1 Move city2 : 军队A从city ...
- 利用window.performance.timing进行性能分析
性能分析... window.performance.timing中相关属性语义: // .navigationStart 准备加载页面的起始时间 // .unloadEventStart 如果前一个 ...
- 解决Docker安装MySQL不区分大小写问题
Docker安装MySQL忽略大小写问题的问题连接MySQL: 查看当前mysql的大小写敏感配置 show global variables like '%lower_case%'; +------ ...