poj 3304 Segments(计算几何基础)

Segments

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11593   Accepted: 3657

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!

Source

Amirkabir University of Technology Local Contest 2006

【题意】

  给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!。
【思路】

  如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为问是否存在一条线和所有线段相交
  若存在一条直线与所有线段相机相交,此时该直线必定经过这些线段的某两个端点,所以枚举任意两个端点即可

【代码】

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define FOR(a,b,c) for(int a=(b);a<(c);a++)
 5 using namespace std;
 6
 7 const int N = 100+10;
 8 const double eps = 1e-8;
 9
10 int dcmp(double x) {
11     if(x<eps) return 0; else return x<0? -1:1;
12 }
13
14 struct Line {
15     double x1,y1,x2,y2;
16 }L[N];
17
18 int n;
19
20 double cross(double x1,double y1,double x2,double y2,double x,double y) {
21     return (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
22 }
23 bool judge(double x1,double y1,double x2,double y2) {
24     if(!dcmp(x2-x1) && !dcmp(y2-y1)) return 0;
25     FOR(i,0,n) {
26         if(cross(x1,y1,x2,y2,L[i].x1,L[i].y1)*
27             cross(x1,y1,x2,y2,L[i].x2,L[i].y2)>eps) return 0;
28     }
29     return 1;
30 }
31
32 int main() {
33     int T;
34     scanf("%d",&T);
35     while(T--) {
36         scanf("%d",&n);
37         FOR(i,0,n)
38             scanf("%lf%lf%lf%lf",&L[i].x1,&L[i].y1,&L[i].x2,&L[i].y2);
39         if(n==1) { puts("Yes!"); continue; }
40         bool ans=0;
41         FOR(i,0,n) {
42             FOR(j,i+1,n) {
43                 if(judge(L[i].x1,L[i].y1,L[j].x1,L[j].y1)) ans=1;
44                 if(judge(L[i].x1,L[i].y1,L[j].x2,L[j].y2)) ans=1;
45                 if(judge(L[i].x2,L[i].y2,L[j].x1,L[j].y1)) ans=1;
46                 if(judge(L[i].x2,L[i].y2,L[j].x2,L[j].y2)) ans=1;
47                 if(ans) break;
48             }
49             if(ans) break;
50         }
51         if(ans) puts("Yes!");
52         else puts("No!");
53     }
54     return 0;
55 }
时间: 2024-10-11 04:45:32

poj 3304 Segments(计算几何基础)的相关文章

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

POJ 3304  Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以在那里作一条线,那么这条线就和所有线段都至少有一个交点,所以如果有一条直线和所有线段都有交点的话,那么就一定有解. 怎么确定有没直线和所有线段都相交?怎么枚举这样的直线?思路就是固定两个点,这两个点在所有线段上任意取就可以,然后以这两个点作为直线,去判断其他线段即可.为什么呢?因为如果有直线和所有线段都相

POJ 3304 Segments(计算几何:直线与线段相交)

POJ 3304 Segments 大意:给你一些线段,找出一条直线能够穿过所有的线段,相交包括端点. 思路:遍历所有的端点,取两个点形成直线,判断直线是否与所有线段相交,如果存在这样的直线,输出Yes,但是注意去重. struct Point { double x, y; } P[210]; struct Line { Point a, b; } L[110]; double xmult(Point p1, Point p2, Point p) { return (p1.x-p.x)*(p2.

POJ 3304 Segments 线段和直线的交点

C - Segments Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3304 Appoint description:  System Crawler  (2016-05-07) Description Given n segments in the two dimensional space, write a program, w

POJ 3304 Segments 基础线段交判断

LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断相交先求叉积再用跨立定理.枚举每个线段的起始结束点作为直线起点终点遍历即可. /** @Date : 2017-07-12 14:35:44 * @FileName: POJ 3304 基础线段交判断.cpp * @Platform: Windows * @Author : Lweleth ([em

POJ 3304 Segments 【计算几何】【直线和线段的关系】

题目链接:http://poj.org/problem?id=3304 题目大意:T个case,每个case里面有N条线段,判断能否存在一条直线,使得所有的线段在这条直线上都能有公共点,如果存在输出Yes,否则输出No. 题目的意思可以变成,在N条直线的2*N个端点中选择两个点组成一条直线能否满足这个条件,暴力枚举即可,注意的一点是在枚举的2*n个点中每次选择的两个点要判断是不是重复的. #include<iostream> #include<stdio.h> #include&l

POJ 3304 Segments(判断直线与线段是否相交)

http://poj.org/problem?id=3304 等价于是否存在一条直线穿过所有线段 判断直线与线段是否相交: 首先用两点p1,p2确定了一条直线 在用p1,p2分别与计算线段两个端点计算叉乘即可 ,叉乘之积>0就说明线段两端点在直线的同侧,也就是直线不经过此线段 注意顺序不要搞反了 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #incl

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

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13514   Accepted: 4331 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

POJ 3304 segments 线段和直线相交

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14178   Accepted: 4521 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

[POJ 3304]Segments

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14918   Accepted: 4728 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