我是靠谱客的博主 喜悦超短裙,最近开发中收集的这篇文章主要介绍1039:Pipe,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

描述

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.

输入

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.

输出

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.

样例输入

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

样例输出

4.67

Through all the pipe.

来源

Central Europe 1995

直接上代码:

#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("%.2fn",ans+eps);
    }
    return 0;
}

最后

以上就是喜悦超短裙为你收集整理的1039:Pipe的全部内容,希望文章能够帮你解决1039:Pipe所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部