poj 1039 Pipe

Pipe
Time Limit: 1000MS Memory Limit: 10000K
Total 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

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.

Source

提示

题意:

给出几个拐点的坐标,依次连线作为管子的上壁,点的纵坐标减一之后依次连线作为管子的下壁,我们能否让光线穿过管子(没有反射),不能就输出最大的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 Code

Problem: 1039		Code Length: 2229B
Memory: 404K		Time: 47MS
Language: GCC		Result: 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,y;
    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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值