poj 1039 Pipe

来源:互联网 发布:全球工业软件市场规模 编辑:程序博客网 时间:2024/05/01 04:17
Pipe
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10358 Accepted: 3207

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

提示

题意:

给出几个拐点的坐标,依次连线作为管子的上壁,点的纵坐标减一之后依次连线作为管子的下壁,我们能否让光线穿过管子(没有反射),不能就输出最大的x坐标,否则输出“Through all the pipe.”。

思路:

我们枚举一个上点和一个下点,看是否能穿过管子,思路不难,难的是如何去敲,没模板很痛苦呢。

有公共端点o叉乘,并判拐:

double cross(struct point a,struct point b,struct point o){    return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);}
判断两条线段是否相交,严格相交是不算端点的,但这个代码包括端点:

int check(struct point a,struct point b,struct point c,struct point d){    double t;    t=cross(c,b,a)*cross(d,b,a);    if(t<0||fabs(t)<1e-4)    {        return 1;    }    else    {        return 0;    }}
直线相交的交点:
void intersection(struct point a,struct point b,struct point c,struct point d){    double A1,B1,C1,A2,B2,C2,x;    A1=b.y-a.y;    B1=a.x-b.x;    C1=b.x*a.y-a.x*b.y;    A2=d.y-c.y;    B2=c.x-d.x;    C2=d.x*c.y-c.x*d.y;    p.x=(B1*C2-B2*C1)/(A1*B2-A2*B1);//假设p点是交点    p.y=(A2*C1-A1*C2)/(A1*B2-A2*B1);}

示例程序

Source CodeProblem: 1039Code Length: 2229BMemory: 404KTime: 47MSLanguage: GCCResult: Accepted#include <stdio.h>#include <math.h>struct point{    double x,y;}p[20];double cross(struct point a,struct point b,struct point o){    return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);}int check(struct point a,struct point b,struct point c){    struct point d;//d点是下壁拐点    double t;    b.y--;//b点是下壁拐点    d.x=c.x;    d.y=c.y-1;    t=cross(c,b,a)*cross(d,b,a);    if(t<0||fabs(t)<1e-4)    {        return 1;    }    else    {        return 0;    }}double intersection(struct point a,struct point b,struct point c,struct point d,int flag){    double A1,B1,C1,A2,B2,C2,x;    if(flag==1)//cd段是下壁,上壁向下平移就是下壁了    {        c.y--;        d.y--;    }    b.y--;//b点是下壁拐点    A1=b.y-a.y;    B1=a.x-b.x;    C1=b.x*a.y-a.x*b.y;    A2=d.y-c.y;    B2=c.x-d.x;    C2=d.x*c.y-c.x*d.y;    x=(B1*C2-B2*C1)/(A1*B2-A2*B1);    y=(A2*C1-A1*C2)/(A1*B2-A2*B1);    return x;}int main(){    int n,i,i1,i2;    double x,t;    scanf("%d",&n);    while(n!=0)    {        for(i=0;n>i;i++)        {            scanf("%lf %lf",&p[i].x,&p[i].y);        }        x=p[0].x;//p[0].x初始最大值        for(i=0;n>i;i++)        {            for(i1=0;n>i1;i1++)            {                if(i!=i1)                {                    for(i2=0;n>i2;i2++)                    {                        if(check(p[i],p[i1],p[i2])==0)                        {                            break;                        }                    }                    if(i2==n)//可以通过管子就不用判断了                    {                        break;                    }                    else if(i2>i&&i2>i1)//如果条件不成立说明这种方法不存在                    {                        t=intersection(p[i],p[i1],p[i2-1],p[i2],0);//光线与上壁相交的交点横坐标                        if(t>x)//更新最大值                        {                            x=t;                        }                        t=intersection(p[i],p[i1],p[i2-1],p[i2],1);//光线与下壁相交的交点横坐标                        if(t>x)//更新最大值                        {                            x=t;                        }                    }                }            }            if(i2==n)            {                break;            }        }        if(i2==n)        {            printf("Through all the pipe.\n");        }        else        {            printf("%.2f\n",x);        }        scanf("%d",&n);    }    return 0;}
                                             
0 0
原创粉丝点击