題意:給出n條線段,判斷是否存在有一條直線,滿足所有的線段在直線上投影后至少有一個公用點(與所有線段都會相交)
開始想錯了,因為是再已經給的線段中 是否存在選一條做直線,使得它與所有線段相交;
這樣也能過sample input;
害我找了一天了;
#include <iostream>#include <cmath>#include <cstdlib>#include <cstdio>using namespace std;const double eps = 1e-8;const int maxn = 100+5;struct Point{ double x; double y; Point(double a = 0, double b = 0){ x = a; y = b; }} P[maxn*2];struct Line{ Point u; Point v;} L;int T,N;int Sig(double x) { return (x > eps) - (x < -eps);// return x < -eps? -1 : x > eps; //這個也行}double Mult(Point p1, Point p2) // 兩點 叉積{ return p1.x*p2.y - p1.y*p2.x;}double Mult(Point p0, Point p1, Point p2) //三點 叉積{ return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y);}int same_site(Point p1, Point p2, Line L) //point p1 & p2 on the same site of line L{ int a = Sig(Mult(L.u,L.v,p1)); int b = Sig(Mult(L.u,L.v,p2)); return a*b > 0;}int Judge(Line L){ if(Sig(L.u.x - L.v.x)==0 && Sig(L.u.y - L.v.y)==0) return false; for(int i = 1; i < 2*N; i += 2) if(same_site(P[i],P[i+1],L)) return false; return true;}int slove(){ for(int i = 1; i < N*2; i++) { for(int j = i+1; j <= N*2; j++) { L.u.x = P[i].x; L.u.y = P[i].y; L.v.x = P[j].x; L.v.y = P[j].y; if(Judge(L))//如果成功,即找到了這條直線 return true; } } return false;}int main(){#ifndef ONLINE_JUDGE freopen("in","r",stdin);#endif cin>>T; while(T--) { cin>>N; for(int i = 1; i <= N*2; i++) { cin>>P[i].x>>P[i].y; } if(slove()) puts("Yes!"); else puts("No!"); }}