1. 程式人生 > >POJ3304:Segments——題解

POJ3304:Segments——題解

交點 如果 ctype cnblogs def amp blank 線段 是否

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

題目大意:給n條線段,求是否存在一條直線,將所有線段投影到上面,使得所有投影至少交於一點。

————————————————————————————

首先考慮當情況可能時,過相交點做垂線,則垂線一定與所有線相交。

所以就變成了求是否存在一條直線,使得直線和所有直線都相交的問題了。

顯然如果存在這樣的線,那麽至少有一種情況,這樣的線的兩個端點是其中兩條直線的任意兩個端點。

那麽枚舉兩個端點判斷即可。

https://www.cnblogs.com/wuwangchuxin0924/p/6218494.html 如何判斷兩直線相交。

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<vector>
#include<cmath>
#include
<algorithm> using namespace std; typedef double dl; const dl eps=1e-8; const int N=101; struct point{//既是向量又是點 dl x; dl y; }p[2*N]; int n; inline point getmag(point a,point b){ point s; s.x=b.x-a.x;s.y=b.y-a.y; return s; } inline dl multiX(point a,point b){
return a.x*b.y-b.x*a.y; } bool check(point a,point b){ if(fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps)return 0; for(int i=1;i<=n;i++){ if(multiX(getmag(a,p[i]),getmag(a,b))*multiX(getmag(a,p[i+n]),getmag(a,b))>eps)return 0; } return 1; } int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i+n].x,&p[i+n].y); } bool flag=0; for(int i=1;i<=2*n&&!flag;i++){ for(int j=i+1;j<=2*n&&!flag;j++){ if(check(p[i],p[j]))flag=1; } } if(flag)puts("Yes!"); else puts("No!"); } return 0; }

POJ3304:Segments——題解