POJ-1039-Pipe

Pipe
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10234 Accepted: 3161

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.

给出一个曲折的管道,求出光线能够到达的管道的最远点的横坐标。

枚举一个上点和一个下点, 确定一条光线(不与管道擦边的光线通过旋转后会与一个上点一个下点擦边)

将每一对上点,下点当作一条线段

从左到右判断这条光线是否可以与所有线段相交, 如果有一条不行说明当前这条光线无法通过管道

那么这条光线应该是在当前线段与上一条线段之间与管道相交, 即当前光线所能到达的最远距离

注意eps, 要进行近似处理, 不然会WA

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#define eps 1e-5
using namespace std;
const int MAX = 21;
const double inf = 9999999;
struct node
{
    double x, y;
}up[MAX], down[MAX];
bool check(node a, node b, node c1, node c2)// 判断当前光线与该线段相交
{
    double y3 = (b.y-a.y)*(c1.x-a.x)/(b.x-a.x)+a.y;
    if(y3>=c2.y-eps&&y3<=c1.y+eps)return true;
    else return false;
}
double dis2(node a, node b, node c, node d)如果不能相交, 求该光线的最远距离
{
    node ret = a;
    double 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));
        ret.x+=(b.x-a.x)*t;
        ret.y+=(b.y-a.y)*t;
        return ret.x;
}
double dis(node a, node b, int k)
{
    return max(dis2(a, b, up[k-1], up[k]), dis2(a, b, down[k-1], down[k]));
}
int main()
{
    int n;
    while(~scanf("%d", &n)&&n)
    {
        for(int i = 1; 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;
        }
        bool flag = false;
        double perfect = -inf;
        for(int i = 1; i<=n&&!flag;++i)
        {
            for(int j = 1; j<=n&&!flag;++j)
            {
                if(i!=j)
                {
                    int k;
                    for(k = 1; k<=n;++k)
                        if(!check(up[i], down[j], up[k], down[k]))break;
                    if(k==n+1)flag = true;
                    else if(k>1)
                        {
                            double ll = dis(up[i], down[j], k);
                            if(ll>perfect)perfect = ll;
                        }
                }
            }
        }
        if(flag)printf("Through all the pipe.\n");
        else printf("%.2f\n", perfect);

    }


    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值