POJ - 3304 Segments(简单几何)
生活随笔
收集整理的這篇文章主要介紹了
POJ - 3304 Segments(简单几何)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出n條線段,現在問是否存在一條直線,使得所有線段向這條直線的投影存在一個共同的交點
題目分析:題意有點抽象,需要轉換一下,因為所有的線段向某一條直線的投影存在一個交點,那么在那條直線上,從交點位置開始,沿著垂直于直線的方向做另一條直線,會發現這條直線與n條線段都存在一個交點,也就是都相交,這樣題目就轉換為了是否存在一條直線,使得與n條線段都有交點
因為直線也是由兩個點組成的,我們枚舉所有線段上的點,作為構成直線上的兩個點,每次判斷一下是否滿足條件即可,時間復雜度為(2*n)*(2*n)*n,因為n比較小,所以直接實現就行了
有個細節需要注意一下,可能會有兩個點重合的情況,這個時候需要特判一下
代碼:
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<set> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=110;const double eps = 1e-8;int n;int sgn(double x){if(fabs(x) < eps)return 0;if(x < 0)return -1;else return 1; }struct Point{double x,y;Point(){}Point(double _x,double _y){x = _x;y = _y;}void input(){scanf("%lf%lf",&x,&y);}double distance(Point p){return hypot(x-p.x,y-p.y);}Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);}//叉積double operator ^(const Point &b)const{return x*b.y - y*b.x;}//點積double operator *(const Point &b)const{return x*b.x + y*b.y;} };struct Line{Point s,e;Line(){}Line(Point _s,Point _e){s = _s;e = _e;}void input(){s.input();e.input();}//`直線和線段相交判斷`//`-*this line -v seg`//`2 規范相交`//`1 非規范相交`//`0 不相交`int linecrossseg(Line v){int d1 = sgn((e-s)^(v.s-s));int d2 = sgn((e-s)^(v.e-s));if((d1^d2)==-2) return 2;return (d1==0||d2==0);} }line[N];bool check(Line l) {if(sgn(l.s.distance(l.e))==0)return false;for(int i=1;i<=n;i++)if(!l.linecrossseg(line[i]))return false;return true; }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int w;cin>>w;while(w--){vector<Point>point;scanf("%d",&n);for(int i=1;i<=n;i++){line[i].input();point.push_back(line[i].s);point.push_back(line[i].e);}for(int i=0;i<point.size();i++)for(int j=0;j<point.size();j++)if(check(Line(point[i],point[j]))){puts("Yes!");goto end;}puts("No!"); end:;}return 0; }?
總結
以上是生活随笔為你收集整理的POJ - 3304 Segments(简单几何)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ - 2318 TOYS(叉积+二
- 下一篇: POJ - 1696 Space Ant