Segments

http://poj.org/problem?id=3304

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18066   Accepted: 5680

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 (x1y1) and (x2y2) 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! 枚举每两个线段的端点,判断这些端点形成的直线是否与线段都有交点
还有,题目说如果两个点的距离小于1e-8就看成一个点,所以要考虑重点
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std; struct Point{
double x,y;
}; struct Line{
Point s,e;
}line[]; double Cross(double x0,double y0,double x1,double y1,double x2,double y2){
return (x1-x0)*(y2-y0)-(y1-y0)*(x2-x0);
} double distance(double x1,double y1,double x2,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
} bool Check(double x1,double y1,double x2,double y2,int n){
if(distance(x1,y1,x2,y2)<0.00000001) return ;
for(int i=;i<=n;i++){
if(Cross(x1,y1,x2,y2,line[i].s.x,line[i].s.y)*Cross(x1,y1,x2,y2,line[i].e.x,line[i].e.y)>0.00000001){
return ;
}
}
return ;
} bool ac(int n){
Line tmp;
int co=;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
tmp.s=line[i].s;
tmp.e=line[j].s;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
tmp.s=line[i].s;
tmp.e=line[j].e;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
tmp.s=line[i].e;
tmp.e=line[j].s;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
tmp.s=line[i].e;
tmp.e=line[j].e;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
} }
if(co)
return true;
return false;
} int main(){ int T;
cin>>T;
while(T--){
int n;
cin>>n;
Point s,e;
for(int i=;i<=n;i++){
cin>>s.x>>s.y>>e.x>>e.y;
line[i].s=s;
line[i].e=e;
}
if(ac(n)){
puts("Yes!");
}
else{
puts("No!");
}
} }

Segments(叉积)的更多相关文章

  1. POJ 3304 Segments 叉积

    题意: 找出一条直线,让给出的n条线段在这条直线的投影至少有一个重合的点 转化一下,以重合的点作垂线,那么这条直线一定经过那n条线段.现在就是求找到一条直线,让这条直线经过所有线段 分析: 假设存在这 ...

  2. POJ 3304 Segments【叉积】

    题意:有n条线段,问有没有一条直线使得所有线段在这条直线上的投影至少有一个共同点. 思路:逆向思维,很明显这个问题可以转化为是否有一条直线穿过所有线段,若有,问题要求的直线与该直线垂直,并且公共点为垂 ...

  3. poj 3304 Segments

    Segments 题意:给你100以内的n条线段,问你是否存在一条直线,使得题给的线段在这条直线上的“投影” 相交于一点: 思路: 1.先要将线段投影相交于一点转变为存在一条直线与所有的线段相交: 很 ...

  4. POJ 3304 Segments[直线与线段相交]

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13514   Accepted: 4331 Descrip ...

  5. (叉积,线段判交)HDU1086 You can Solve a Geometry Problem too

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  6. POJ 3304 Segments(线的相交判断)

    Description Given n segments in the two dimensional space, write a program, which determines if ther ...

  7. POJ 3304 Segments 判断直线和线段相交

    POJ 3304  Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以 ...

  8. [LeetCode] Number of Segments in a String 字符串中的分段数量

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  9. Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor

    结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...

随机推荐

  1. [转]win server 2003 + IIS 6 搭建MVC 运行环境

    本文来自:http://c.jinhusns.com/bar/t-993 win server 2003 + IIS 6 搭建MVC 运行环境 上一篇 下一篇近乎_问阳 发表于:2014-01-07 ...

  2. javascript的防篡改对象之preventExtensions()方法

    js在默认情况下,所有的对象都是可扩展的.这也是让很多开发人员头特疼的问题.因为在同一环境中,一不小心就会发生修改了不必要的对象,而自己却不知道. 在ECMAScript5可以解决这种问题了. pre ...

  3. eventql操作脚本

    a) standalone mode mkdir -p /var/evql/standalone/usr/local/bin/evqld --standalone --datadir /var/evq ...

  4. Java反射机制 —— 简单了解

    一.概述 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为jav ...

  5. Python环境搭建之OpenCV

    一.openCV介绍 Open Source Computer Vision Library.OpenCV于1999年由Intel建立,如今由Willow Garage提供支持.OpenCV是一个基于 ...

  6. python入门-WHILE循环

    1 使用while循环 current_number= : print(current_number) current_number += 2 让用户选择退出 prompt = "tell ...

  7. OpenCL 归约 1

    ▶ 照着书上的代码,写了几个一步归约的计算,只计算一步,将原数组归约到不超过 1024 个工作项 ● 代码 // kernel.cl __kernel void reduce01(__global u ...

  8. Procedure-Function mysql

    1.基本用法 drop PROCEDURE if EXISTS sp1; -- 如果存在sp1存储过程则删除掉 create PROCEDURE sp1() SELECT 1; --创建最简单的存储过 ...

  9. ABAP-反调JCO服务

  10. 不定宽高的DIV,垂直水平居中

    1.怎么让一个不定宽高的DIV,垂直水平居中? 答:1)使用CSS方法. 父盒子设置: display:table-cell; text-align:center; vertical-align:mi ...