POJ 1039 Pipe

来源:互联网 发布:ug8.5数控编程实例 编辑:程序博客网 时间:2024/04/30 21:46
Pipe
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 8010 Accepted: 2379

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.

Source

Central Europe 1995
   这题我在得知思路: 枚举上下两个点就开始写。 一开始有一个问题一直困扰着我,就是怎么判断交叉,慢慢的我发现其实 直线在两个边界上的y ,都在边界上的 y的范围之内 这条线就一定在管内。 否则就出线了交叉。 假设左边界都已经判断过了,都在管内,那么对于右边界 x, 根据直线计算出的y 大于上边界就会与上边交叉, 过小就会与下边交叉, 都不是的话,就说明在管内
   注意精度问题啊,试了很久才ac了 尤其注意x<y  在卡精度的时候的写法
#include <stdio.h>#include <string.h>#include <math.h>#define EQS 1e-7struct{    double x,y;}up[100];struct{    double x,y;}down[100];int INF=0x7ffffff;int main(){    int i,j,n,m,s,t,z;    double k,d,max,x1,y1,x2,y2,k1,d1;    while(scanf("%d",&n)!=EOF)    {        if(n==0)        {            break;        }        for(i=0;i<=n-1;i++)        {            scanf("%lf %lf",&up[i].x,&up[i].y);            down[i].x = up[i].x;            down[i].y = up[i].y - 1;        }        max=(double)(-1*INF);        for(i=0;i<=n-1;i++)        {            for(j=0; j<=n-1; j++)            {                if(i==j)                {                    continue;                }                x1 = up[i].x ;                y1 = up[i].y ;                x2 = down[j].x ;                y2 = down[j].y ;                k = (y2 - y1) / (x2 - x1);                d = y1 - k * x1;                y1 = k * up[0].x + d;                if(!(y1 < up[0].y||fabs(y1-up[0].y)<=EQS)||!(y1 > down[0].y||fabs(y1-down[0].y)<=EQS))                {                    continue;                }                for(z=0; z<=n-2; z++)                {                    y1= k * up[z+1].x + d;                    if(!(y1 < up[z+1].y||fabs(y1-up[z+1].y)<=EQS))                    {                        x1 = up[z].x;                        y1 = up[z].y;                        x2 = up[z+1].x;                        y2 = up[z+1].y;                        k1 = (y2 - y1) / (x2 - x1);                        d1 = y1 - k1 * x1;                        x1 = (d1 - d)/(k - k1);                        if(x1>max)                        {                            max=x1;                        }                        break;                    }else if(!(y1 > down[z+1].y||fabs(y1-down[z+1].y)<=EQS))                    {                        x1 = down[z].x;                        y1 = down[z].y;                        x2 = down[z+1].x;                        y2 = down[z+1].y;                        k1 = (y2 - y1) / (x2 - x1);                        d1 = y1 - k1 * x1;                        x1 = (d1 - d)/(k - k1);                        if(x1>max)                        {                            max=x1;                        }                        break;                    }                }                if(z==n-1)                {                    break;                }            }            if(j!=n)            {                break;            }        }        if(i!=n)        {            printf("Through all the pipe.\n");        }else        {            printf("%.2lf\n",max);        }    }    return 0;}

 
原创粉丝点击