[ACM_幾何] Pipe

 
  
   本題大意:  給定一個管道上邊界的拐點,管道寬爲1,求一束光最遠能照到的地方的X座標,若是能照到終點,則輸出...
  解題思路:  若想照的最遠,則光線必過某兩個拐點,所以用二分法對全部拐點對進行枚舉,找出最遠大值便可。
#include<iostream>
#include<cmath>
#include<string.h>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>

using namespace std;
#define eps 1e-8
#define PI acos(-1.0)


//點和向量
struct Point{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
    void out(){cout<<"("<<x<<','<<y<<") ";}
};
typedef Point Vector;
Vector operator+(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator-(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator*(Vector a,double p){return Vector(a.x*p,a.y*p);}
Vector operator/(Vector a,double p){return Vector(a.x/p,a.y/p);}
bool operator<(const Vector& a,const Vector& b){return a.x<b.x||(a.x==b.x && a.y<b.y);}
int dcmp(double x){
    if(fabs(x)<eps)return 0;
    else return x<0 ? -1:1;
}
bool operator==(const Point& a,const Point& b){
    return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;
}
double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}//向量點積
double Length(Vector A){return sqrt(Dot(A,A));}//向量模長
double Angle(Vector A,Vector B){return acos(Dot(A,B)/Length(A)/Length(B));}//向量夾角
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);}//三角形面積的2倍
//繞起點逆時針旋轉rad度
Vector Rotate(Vector A,double rad){          
    return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
double torad(double jiao){return jiao/180*PI;}//角度轉弧度
double tojiao(double ang){return ang/PI*180;}//弧度轉角度 
//單位法向量
Vector Normal(Vector A){
    double L=Length(A);
    return Vector(-A.y/L,A.x/L);
}
//點和直線
struct Line{
    Point P;//直線上任意一點
    Vector v;//方向向量,他的左邊對應的就是半平面
    double ang;//極角,即從x正半軸旋轉到向量v所需的角(弧度)
    Line(){}
    Line(Point p,Vector v):P(p),v(v){ang=atan2(v.y,v.x);}
    bool operator<(const Line& L)const {
        return ang<L.ang;
    }
};
//計算直線P+tv和Q+tw的交點(計算前必須確保有惟一交點)即:Cross(v,w)非0
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w){
    Vector u=P-Q;
    double t=Cross(w,u)/Cross(v,w);
    return P+v*t;
}
double getx(Point p1,Point p2,Point p3,Point p4)//找到直線和線段相交的橫座標(求兩直線交點)
 {
     double k1=(p2.y-p1.y)/(p2.x-p1.x);
     double k2=(p4.y-p3.y)/(p4.x-p3.x);
     double b1=p2.y-k1*p2.x;
     double b2=p3.y-k2*p3.x;
     return (b2-b1)/(k1-k2);
 }
//點到直線距離(dis between point P and line AB)
double DistanceToLine(Point P,Point A,Point B){
    Vector v1=B-A , v2=P-A;
    return fabs(Cross(v1,v2))/Length(v1);
}
//dis between point P and segment AB
double DistancetoSegment(Point P,Point A,Point B){
    if(A==B)return Length(P-A);
    Vector v1=B-A,v2=P-A,v3=P-B;
    if(dcmp(Dot(v1,v2))<0)return  Length(v2);
    else if(dcmp(Dot(v1,v3))>0)return Length(v3);
    else return fabs(Cross(v1,v2))/Length(v1);
}
//point P on line AB 投影點
Point GetLineProjection(Point P,Point A,Point B){
    Vector v=B-A;
    return A+v*(Dot(v,P-A)/Dot(v,v));
}
//線段規範相交(只有一個且不在端點)每條線段兩端都在另外一條兩側,(叉積符號不一樣)
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){
    a2=(a2-a1)*10000000000+a1;//射線A1A2相交線短B1B2
    double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
           c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
//判斷點P是否在線段AB上
bool OnSegment(Point p,Point a1,Point a2){
    return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p))<0;
}
//多邊形的面積(能夠是非凸多邊形)
double PolygonArea(Point* p,int n){
    double area=0;
    for(int i=1;i<n-1;i++)
        area+=Cross(p[i]-p[0],p[i+1]-p[0]);
    return area/2;
}


//點p在有向直線左邊,上面不算
bool OnLeft(Line L,Point p){
    return Cross(L.v,p-L.P)>0;
}

double ok(double x,double y,double d,double z){
    double f=fabs(d*(1/tan(acos(z/y))+1/tan(acos(z/x))))-z;
    if(fabs(f)<1e-4)return  0;
    else return f;
}
//計算凸包輸入點數組p,個數n,輸出點數組ch,返回凸包定點數
//輸入不能有重複,完成後輸入點順序被破壞
//若是不但願凸包的邊上有輸入點,把兩個<=改爲<
//精度要求高時,建議用dcmp比較
//基於水平的Andrew算法-->一、點排序二、刪除重複的而後把前兩個放進凸包
//三、從第三個日後當新點在凸包前進左邊時繼續,不然一次刪除最近加入的點,直到新點在左邊
int ConVexHull(Point* p,int n,Point*ch){
    sort(p,p+n);
    int m=0;
    for(int i=0;i<n;i++){//下凸包
        while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
        ch[m++]=p[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--){//上凸包
        while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
        ch[m++]=p[i];
    }
    if(n>1)m--;
    return m;
}

//******************************************************************************
#define N 21
#define left -10e8
Point up[N],down[N];
int n;

double getans()//最值必定過兩個頂點一上一下,因此枚舉全部點對
 {
     int i,j,k;
     double ans=left,right;//二分法
     double tx,ty;
     Point ql,qr;
     for(i=0;i<n;i++)
     for(j=0;j<n;j++)
     {
         if(i==j)continue;
         ql=up[i];
         qr=down[j];
         right=left;//left是左邊界,很是小的一個值,right就是枚舉的過兩點的直線最遠能達的x的大小
         for(k=0;k<n;k++)//驗證枚舉直線是否知足全部點
         {
             tx=up[k].x;
             ty=(tx-ql.x)*(qr.y-ql.y)/(qr.x-ql.x)+ql.y;//求出對應x點在枚舉的直線上的y值 
             if(ty>down[k].y&&ty<up[k].y||fabs(ty-down[k].y)<eps||fabs(ty-up[k].y)<eps)//該y值應在上下之間或與上或下重合
             right=tx;//更新有邊界值
             else//若是該點時不知足 
             {
                 if(k)//細節!!!
                 {
                     if(ty<down[k].y)
                     right=getx(ql,qr,down[k-1],down[k]);
                     else
                     right=getx(ql,qr,up[k-1],up[k]);
                 }
                 break;
             }
         }
         if(right>ans)
             ans=right;
     }
     return ans;
 }

int main(){
    cout.precision(2);
    for(;cin>>n&&n;){
        for(int i=0;i<n;i++){
            cin>>up[i].x>>up[i].y;
            down[i].x=up[i].x;
            down[i].y=up[i].y-1;
        }
        double ans=getans();
        if(ans>up[n-1].x||fabs(ans-up[n-1].x)<eps)
        cout<<"Through all the pipe.\n";
        else cout<<fixed<<ans<<'\n';
    }return 0;
}
View Code
相關文章
相關標籤/搜索