POJ 1039 Pipe

POJ 1039 Pipe

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.

判断直线和线段相交的题目,一上一下两点构成的线段一定是最优的,枚举判断即可:

#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
double eps=1e-8;
struct point{
    double x,y;
}up[25],down[25];

struct segment{
    point s,e;
};

int sgn(double x){
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}

double cross(point a,point b,point c){
    return (a.x-b.x)*(b.y-c.y)-(a.y-b.y)*(b.x-c.x);
}

bool line_seg(point a1,point a2,segment L){//判直线和线段相交
    return sgn(cross(a1,L.s,a2))*sgn(cross(a1,L.e,a2))<=0;
}

point getIntersect(point a, point b, point c, point d) {//判断两直线交点
    point ans;
    double A1 = b.y - a.y;
    double B1 = a.x - b.x;
    double C1 = (b.x - a.x) * a.y - (b.y - a.y) * a.x;
    double A2 = d.y - c.y;
    double B2 = c.x - d.x;
    double C2 = (d.x - c.x) * c.y - (d.y - c.y) * c.x;
    ans.x = (C2 * B1 - C1 * B2) / (A1 * B2 - A2 * B1);
    ans.y = (C1 * A2 - C2 * A1) / (A1 * B2 - A2 * B1);
    return ans;
 }

int main(){
    int n;
    while(cin>>n && n){
        for(int 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;
        }
        int flag,k;
        double ans=-1e9;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==j) continue;
                flag=1;
                for(k=0;k<n;k++){
                    segment S;
                    S.s=up[k],S.e=down[k];
                    if(!line_seg(up[i],down[j],S)) {flag=0;break;}
                }
                if(flag) break;
                else if(k>max(i,j)){//这个条件很重要,因为只有相交的线段下标在直线右端才能更新答案
                    point P;
                    P=getIntersect(up[i],down[j],up[k-1],up[k]);
                    ans=max(ans,P.x);
                    P=getIntersect(up[i],down[j],down[k-1],down[k]);
                    ans=max(ans,P.x);
                }
            }
            if(flag) break;
        }
        if(flag) puts("Through all the pipe.");
        else printf("%.2f\n",ans+eps);
    }
    return 0;
}
发布了296 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43765333/article/details/104388779