POJ 1039 pipe

来源:互联网 发布:知乎安卓屏幕录像软件 编辑:程序博客网 时间:2024/04/30 22:36
Pipe
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 8839 Accepted: 2666

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


这道题目的重点就是:要想得到最长的光线,必须要经过管道的两个点,所以只需将管道上的转折点遍历一遍就好
然后在遍历的过程中,加一个for循环判断能否通过第k个管口,如果不能通过,则交点一定在第K-1个管口到第k 个管口之间,(左闭右开即考虑3个情况:
                                 1、与第K-1个转折点有交点2、与k-1到k之间的上管壁有交点3、与k-1到k之间的下管壁有交点),
          若横坐标的最大值大于等于最末端管口的横坐标的时候,即光线能通过整个管壁,输出全通过!
#include <stdio.h>#include <math.h>struct point{    double x;    double y;}up[25],down[25];double mul(struct point p1,struct point p2,struct point p3)//叉积公式{    return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);}int pipe1(struct point p1,struct point p2,struct point p3,struct point p4)//判断是否能进入管口{    double d1,d2;    d1=mul(p1,p2,p3);    d2=mul(p1,p2,p4);    if(d1*d2<=0)        return 1;    return 0;}int pipe2(struct point p1,struct point p2,struct point p3,struct point p4)//判断是否与管壁相交{    double d1,d2;    d1=mul(p1,p2,p3);    d2=mul(p1,p2,p4);    if(d1*d2<0)        return 1;    return 0;}double jiaodian(struct point a,struct point b,struct point c,struct point d)//求两线段的交点{   // double t;   // t=((a.x-c.x)*(c.y-d.y)-(a.y-c.y)*(c.x-d.x))/((a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x));   // return a.x+(b.x-a.x)*t;    return (mul(a,b,c)*d.x-mul(a,b,d)*c.x)/(mul(a,b,c)-mul(a,b,d));}int main (void){  // freopen("题目142.txt","r",stdin);    int n,i,j,k;    double d1,d2,max,x;    while(scanf("%d",&n)==1&&n)    {        for(i=0;i<n;i++)        {            scanf("%lf%lf",&up[i].x,&up[i].y);            down[i].x=up[i].x;            down[i].y=up[i].y-1;        }        max=up[0].x;   //初始化        for(i=0;i<n;i++)        {            for(j=0;j<n;j++)            {                if(pipe1(up[i],down[j],up[0],down[0])==0)  //如果第一个管口进不去则继续循环下一个                    continue;                for(k=1;k<n;k++)                {                    d1=mul(up[i],down[j],up[k]);                    d2=mul(up[i],down[j],down[k]);                    if(d1*d2>0)                //判断到哪一个管口失败,则光线必与此管口的左边管壁相交(就是光线射进的那边)                    {                        if(pipe2(up[i],down[j],up[k],up[k-1]))    //如果与上管壁相交                        {                            x=jiaodian(up[i],down[j],up[k],up[k-1]);                            max=(max>x)?max:x;                            break;                        }                        if(pipe2(up[i],down[j],down[k],down[k-1]));     //如果与下管壁相交                        {                            x=jiaodian(up[i],down[j],down[k],down[k-1]);                             max=(max>x)?max:x;                             break;                        }                        max=(max>up[k-1].x)?max:up[k-1].x;    //如果这条光线不与管壁相交而直接相交于转折点处,返回这个转折点的横坐标                        break;                    }                }                if(k==n)                        max=up[n-1].x+1;            }        }        if(max>=up[n-1].x)            printf("Through all the pipe.\n");        else            printf("%.2lf\n",max);    }    return 0;}


0 0
原创粉丝点击