E - Pipe解题报告(来自网络)

E - Pipe
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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

4
0 1
2 2
4 1
6 4
6
0 1
2 -0.6
5 -4.45
7 -5.57
12 -10.8
17 -16.55
0

Sample Output

4.67
Through all the pipe.


这题的题意是一个通光的管道,假设这种材质不反射光,求从入口任意方向进入的光所能达到的最大的横坐标大小,如果有光能够完全通过管道则输出Through all the pipe.否则输出最大的横坐标值,可以证明的是,最长距离的光一定是经过某两个顶点的,假设有有一条光线不经过顶点,那么通过平移,旋转到顶点上之后所能得到的值一定比原来的要大,所以可以枚举每两个顶点的直线,从左到右依次判断是否在拐点的内部,如果不在说明和前一个拐点所购成了两个线段之间有一条是相交的,那么交点就是最大值

View Code 
 #include<iostream>
 #include<cstdio>
 #include<cstring>
 #include<cmath>
 #define N 21
 #define eps 1e-5
 #define left -10e8
 using namespace std;
 struct point
 {
     double x,y;
 };
 point up[N],down[N];
 int n;
 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);
 }
 double getans()
 {
     int i,j,k;
     double ans=left,right;
     double tx,ty;
     point ql,qr,pl,pr;
     for(i=0;i<n;i++)
     for(j=0;j<n;j++)
     {
         if(i==j)
         continue;
         ql=up[i];
         qr=down[j];
         right=left;
         for(k=0;k<n;k++)
         {
             tx=up[k].x;
             ty=(tx-ql.x)*(qr.y-ql.y)/(qr.x-ql.x)+ql.y;
             if(ty>down[k].y&&ty<up[k].y||fabs(ty-down[k].y)<eps||fabs(ty-up[k].y)<eps)
             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()
 {
     int i,j,k;
     while(scanf("%d",&n)&&n)
     {
         for(i=0;i<n;i++)
         {
             scanf("%lf%lf",&up[i].x,&up[i].y);
             down[i]=up[i];
             down[i].y-=1.0;
         }
         double ans=getans();
         if(ans>up[n-1].x||fabs(ans-up[n-1].x)<eps)
         printf("Through all the pipe.\n");
         else
         printf("%.2lf\n",ans);
     }
     return 0;
 }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值