POJ 1039 Pipe

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

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

   这题我在得知思路: 枚举上下两个点就开始写。 一开始有一个问题一直困扰着我,就是怎么判断交叉,慢慢的我发现其实 直线在两个边界上的y ,都在边界上的 y的范围之内 这条线就一定在管内。 否则就出线了交叉。 假设左边界都已经判断过了,都在管内,那么对于右边界 x, 根据直线计算出的y 大于上边界就会与上边交叉, 过小就会与下边交叉, 都不是的话,就说明在管内
   注意精度问题啊,试了很久才ac了 尤其注意x<y  在卡精度的时候的写法
#include <stdio.h>
#include <string.h>
#include <math.h>
#define EQS 1e-7
struct
{
    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;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值