POJ 3304 Segments

Segments
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6615   Accepted: 1981

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

 

计算几何,直线与线段的相交,

题意:要计算是否存在一条直线,所有给出的线段投影到这条直线上的时候,有一个公共的交点。

我们可以这样想,如果存在一条直线满足与所有的直线都有交点的话,那么这条直线的垂线就是符合题意的一条直线,因为在这条直线的每个交点上做一个垂线的话,这个方向就会满足题目的要求,

所以我们可以转为寻找是否存在一条与所有线段都相交的直线,

 

如果存在一条直线与所有的线段相交的话,那么,我们可以通过平移,让这条直线与其中的一条线段的端点重合,再进一步,我们也可以通过平移或转动,让这条直线与另外的一条线段的端点相交。这样,每一条符合题意的直线,必然与线段的两个端点重合

 
View Code
 1 #include<stdio.h>
 2 #include<cmath>
 3 #include<iostream>
 4 using namespace std;
 5 #define eps 1e-8 
 6 struct Point {
 7     double x,y ;
 8 }p[410] ;
 9 double multi(Point p0,Point p1,Point p2){
10     return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y) ;
11 }
12 bool work(int n){
13     int flag ;
14     for(int i=1;i<=n;i++){
15         for(int j=1+i;j<=n;j++){
16             if(abs(p[j].x-p[i].x)>eps || abs(p[j].y-p[i].y)>eps){
17                 flag = 1;
18                 for(int k=1;k<=n;k+=2){
19                     if(multi(p[i],p[j],p[k])*multi(p[i],p[j],p[k+1])> 0){
20                         flag = 0;
21                         break;
22                     }
23                 }
24                 if(flag)     return 1;
25             }
26         }
27     }
28     return 0 ;
29 }
30 int main(){
31     int T;
32     scanf("%d",&T);
33     while(T--){
34         int n;
35         scanf("%d",&n) ;
36         int cnt = 1;
37         for(int i=1;i<=n;i++,cnt+=2)
38             scanf("%lf%lf%lf%lf",&p[cnt].x,&p[cnt].y,&p[cnt+1].x,&p[cnt+1].y) ;    
39         if(work(cnt-1))
40             puts("Yes!") ;
41         else
42             puts("No!") ;
43     }
44     return 0;
45 }

 

posted @ 2012-08-27 16:18  3111006139  阅读(150)  评论(0编辑  收藏  举报