POJ1039-Pipe

来源:互联网 发布:淘宝手机怎么换购 编辑:程序博客网 时间:2024/06/05 16:32
Pipe
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9031 Accepted: 2730

Description

The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape the company ran into the problem of determining how far the light can reach inside each component of the pipe. Note that the material which the pipe is made from is not transparent and not light reflecting.

Each pipe component consists of many straight pipes connected tightly together. For the programming purposes, the company developed the description of each component as a sequence of points [x1; y1], [x2; y2], . . ., [xn; yn], where x1 < x2 < . . . xn . These are the upper points of the pipe contour. The bottom points of the pipe contour consist of points with y-coordinate decreased by 1. To each upper point [xi; yi] there is a corresponding bottom point [xi; (yi)-1] (see picture above). The company wants to find, for each pipe component, the point with maximal x-coordinate that the light will reach. The light is emitted by a segment source with endpoints [x1; (y1)-1] and [x1; y1] (endpoints are emitting light too). Assume that the light is not bent at the pipe bent points and the bent points do not stop the light beam.

Input

The input file contains several blocks each describing one pipe component. Each block starts with the number of bent points 2 <= n <= 20 on separate line. Each of the next n lines contains a pair of real values xi, yi separated by space. The last block is denoted with n = 0.

Output

The output file contains lines corresponding to blocks in input file. To each block in the input file there is one line in the output file. Each such line contains either a real value, written with precision of two decimal places, or the message Through all the pipe.. The real value is the desired maximal x-coordinate of the point where the light can reach from the source for corresponding pipe component. If this value equals to xn, then the message Through all the pipe. will appear in the output file.

Sample Input

40 12 24 16 460 12 -0.65 -4.457 -5.5712 -10.817 -16.550

Sample Output

4.67Through all the pipe.
//AC代码
#include<iostream>#include<queue>#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<cstdio>#include<map>#include<iomanip>#include<cstdlib>const double eps=1e-8;const double INF=9999999;const double inf=7777777;const int Max=101;using namespace std;int sign(double x){    return (x>eps)-(x<-eps);}typedef struct Node{    double x;    double y;}point;point up[Max],down[Max];typedef struct Segment{    point s;    point e;}segment;segment u,v,o;double Distance(point p1,point p2)// 返回两点之间欧氏距离{return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) );}//相交返回true,否则为false,接口为两线段的端点double xmult(point p0,point p1,point p2)//叉积{    //cout<<p0.x<<" "<<p0.y<<" "<<p1.x<<" "<<p1.y<<" "<<p2.x<<" "<<p2.y<<endl;    double p=(p1.x-p0.x)*(p2.y-p0.y);    double q=-(p2.x-p0.x)*(p1.y-p0.y);//建议以后叉积都这样写(不会损失精度,因为你返回的值也是double)return p+q;}bool IsIntersected(point s1,point e1,point s2,point e2)//判断线段相交{return (max(s1.x,e1.x)>=min(s2.x,e2.x))&& (max(s2.x,e2.x)>=min(s1.x,e1.x))&&(max(s1.y,e1.y)>=min(s2.y,e2.y))&&(max(s2.y,e2.y)>=min(s1.y,e1.y))&&(xmult(s1,s2,e1)*xmult(s1,e1,e2)>=0)&&(xmult(s2,s1,e2)*xmult(s2,e2,e1)>=0);}//计算两直线交点,注意事先判断直线是否平行!//线段交点请另外判线段相交(同时还是要判断是否平行!)//计算两直线交点,注意事先判断直线是否平行!//线段交点请另外判线段相交(同时还是要判断是否平行!)point Intersection(point u1,point u2,point v1,point v2)//如果相交返回交点{point res=u1;double a=(u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x);double b=(u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x);if(sign(b)==0)//这里要特判下,如果除数是0,证明两条线段斜率一样并且至少重合一个公共点    {        res.x==INF;        res.y==INF;    }    else    {        double t=a/b;        res.x+=(u2.x-u1.x)*t;        res.y+=(u2.y-u1.y)*t;    }return res;}bool Intersect_p(point u1,point v1,point u2,point v2)//管道口判断{    int d1,d2;    d1=sign(xmult(u1,v1,u2));    d2=sign(xmult(u1,v1,v2));    if(((d1^d2)==-2)||(d1*d2==0))        return true;    return false;}bool Intersect_s(point u1,point v1,point u2,point v2)//管道壁判断{    int d1,d2;    d1=sign(xmult(u1,v1,u2));    d2=sign(xmult(u1,v1,v2));    if((d1^d2)==-2)    return true;    return false;}int main(){    int n,m,i,j,k;    double sum;    int d1,d2;//最长的距离的光线一定是平行x轴,并且这条光线一定是上端点和下端点组成的    point res;    while(cin>>n&&n)    {        for(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;        }        sum=up[0].x;        for(i=0;i<n;i++)        {            for(j=0;j<n;j++)            {                if(!Intersect_p(up[i],down[j],up[0],down[0]))//判断是否能进入第一个管道口                continue;                //能进入第一个管道口                for(k=1;k<n;k++)                {                    d1=sign(xmult(up[i],down[j],up[k]));                    d2=sign(xmult(up[i],down[j],down[k]));                    //cout<<d1<<" "<<d2<<endl;                    if((d1*d2)==1)//如果没有进入第K个管道口则说明交点一定在k-1,k之间                    {                        //cout<<"Yes"<<endl;                        if(Intersect_s(up[i],down[j],up[k],up[k-1]))//交点在上管道壁                        {                            //cout<<"111"<<endl;                            //cout<<i<<" "<<j<<" "<<k<<" "<<k-1<<endl;                            res=Intersection(up[i],down[j],up[k],up[k-1]);                            sum=max(res.x,sum);                            break;                        }                        if(Intersect_s(up[i],down[j],down[k],down[k-1]))//交点在下管道壁                        {                            //cout<<"222"<<endl;                            //cout<<i<<" "<<j<<" "<<k<<" "<<k-1<<endl;                            res=Intersection(up[i],down[j],down[k],down[k-1]);                            sum=max(res.x,sum);                            break;                        }                        //cout<<"333"<<endl;                        //cout<<i<<" "<<j<<" "<<k<<" "<<k-1<<endl;                        sum=max(sum,up[k-1].x);//即不再上又不在下那么只能是端口刚好从上一个管道口上端点射出(仅此一次情况)                        break;                    }                }                //cout<<"k=="<<k<<endl;                if(k==n)                    sum=up[n-1].x+1;            }        }        //cout<<sum<<endl;        if(sum>=up[n-1].x)//如果距离刚好大于或等于最后上端点的距离则说明光线可以全部通过            cout<<"Through all the pipe."<<endl;        else            cout<<setprecision(2)<<setiosflags(ios::fixed)<<sum<<endl;    }    return 0;}

0 0
原创粉丝点击