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

Segments
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9449   Accepted: 2902

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!

 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

简短的题目描述,但题目确实挺难的

 

别人的题解很详细,我就不献丑了,下面题解的连接:http://hi.baidu.com/cloudayc/item/243f3c4c2a687aaadf2a9fb7

证明:坐标系内若干线段,是否存在一条直线与每条线段都有交点

转成叉积计算线段和直线是否相交,注意重点

转自discuss上的证明:from hanjialong

首先题中的要求等价于:存在一条直线l和所有的线段都相交。
证明:若存在一条直线l和所有线段相交,作一条直线m和l垂直,则m就是题中要求的直线,所有线段投影的一个公共点即为垂足。(l和每条线段的交点沿l投影到m上的垂足处)
反过来,若存在m,所有线段在m上的投影有公共点,则过这点垂直于m作直线l,l一定和所有线段相交。

然后证存在l和所有线段相交等价于存在l过某两条线段的各一个端点且和所有线段相交。
充分性显然。必要性:若有l和所有线段相交,则可保持l和所有线段相交,左右平移l到和某一线段交于端点停止(“移不动了”)。然后绕这个交点旋转。也是转到“转不动了”(和另一线段交于其一个端点)为止。这样就找到了一个新的l。

于是本题可归结为枚举两两线段的各一个端点,连一条直线,再判断剩下的线段是否都和这条直线有交点。

 

 

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <math.h>
  7 
  8 using namespace std;
  9 #define eps 1e-8
 10 #define MAXX 1010
 11 
 12 typedef struct point
 13 {
 14     double x;
 15     double y;
 16 }point;
 17 
 18 typedef struct line
 19 {
 20     point st;
 21     point ed;
 22 }line;
 23 
 24 point p[MAXX];
 25 line li[MAXX];
 26 
 27 bool  dy(double x,double y){ return x>y+eps; }
 28 bool  xy(double x,double y){ return x<y-eps; }
 29 bool dyd(double x,double y){ return x>y-eps; }
 30 bool xyd(double x,double y){ return x<y+eps; }
 31 bool  dd(double x,double y){ return fabs(x-y)<eps; }
 32 
 33 double crossProduct(point a,point b,point c)
 34 {
 35     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
 36 }
 37 
 38 double dist(point a,point b)
 39 {
 40     return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
 41 }
 42 
 43 bool onSegment(point a,point b,point c)
 44 {
 45     double maxx=max(a.x,b.x);
 46     double maxy=max(a.y,b.y);
 47     double minx=min(a.x,b.x);
 48     double miny=min(a.y,b.y);
 49     if(dd(crossProduct(a,b,c),0.0) && xyd(c.x,maxx) && dyd(c.x,minx) && xyd(c.y,maxy) && dyd(c.y,miny))
 50         return true;
 51     return false;
 52 }
 53 
 54 bool segIntersect(point p1,point p2,point p3,point p4)
 55 {
 56     double d1=crossProduct(p3,p4,p1);
 57     double d2=crossProduct(p3,p4,p2);
 58     double d3=crossProduct(p1,p2,p3);
 59     double d4=crossProduct(p1,p2,p4);
 60     /*if(xy(d1*d2,0.0) && xy(d3*d4,0.0))
 61         return true;
 62     if(dd(d1,0.0) && onSegment(p3,p4,p1))
 63         return true;
 64     if(dd(d2,0.0) && onSegment(p3,p4,p2))
 65         return true;
 66     if(dd(d3,0.0) && onSegment(p1,p2,p3))
 67         return true;
 68     if(dd(d4,0.0) && onSegment(p1,p2,p4))
 69         return true;
 70     return false;*/
 71     return xyd(d1 * d2,0.0);
 72 }
 73 
 74 int main()
 75 {
 76     int n,m,i,j,t;
 77     //freopen("in.txt","r",stdin);
 78     scanf("%d",&t);
 79     while(t--)
 80     {
 81         scanf("%d",&n);
 82         int sub=0,suc=0;
 83         for(i=0; i<n; i++)
 84         {
 85             scanf("%lf%lf%lf%lf",&p[sub].x,&p[sub].y,&p[sub+1].x,&p[sub+1].y);
 86             sub += 2;
 87             li[suc].st.x=p[sub-2].x;
 88             li[suc].st.y=p[sub-2].y;
 89             li[suc].ed.x=p[sub-1].x;
 90             li[suc++].ed.y=p[sub-1].y;
 91         }
 92         if(n == 1 || n == 2)
 93         {
 94             printf("Yes!\n");
 95             continue;
 96         }
 97         bool flag=false;
 98         for(i=0; i<sub; i++)
 99         {
100             for(j=i+1; j<sub; j++)
101             {
102                 int sum=0;
103                 if(dist(p[i],p[j]) < eps)
104                     continue;
105                 for(int k=0; k<suc; k++)
106                 {
107                     if(!segIntersect(li[k].st,li[k].ed,p[i],p[j]))
108                     {
109                          break;
110                     }
111                     else
112                         sum++;
113                 }//printf("%d***",sum);//test
114                 if(sum >= n)
115                 {
116                     flag=true;
117                     goto end;
118                 }
119                 //else sum=0;
120             }
121         }
122     end:;
123         if(flag)printf("Yes!\n");
124         else printf("No!\n");
125     }
126     return 0;
127 }
View Code