poj1039 几何

链接:http://poj.org/problem?id=1039

Pipe
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9513   Accepted: 2887

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. 
poj1039 几何_第1张图片
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

Central Europe 1995

总结下线段和直线的关系之类的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
using namespace std;
int n;
struct point {
    double x,y;
    point(double x=0,double y=0):x(x),y(y) {}
} s[22],p[22];
const double eps=1e-8;
typedef point vec;
vec operator - (point a,point b) {
    return vec(a.x-b.x,a.y-b.y);
}
vec operator + (point a,point b) {
    return vec(a.x+b.x,a.y+b.y);
}
vec operator * (point a,double t) {
    return vec(a.x*t,a.y*t);
}
int dcmp(double x) {
    if(fabs(x)<=eps) return 0;
    else return x<0?-1:1;
}
double cross(vec a,vec b) {  ///叉积  当前向量逆时针180度为正,顺时针180度为负
    return a.x*b.y-a.y*b.x;  ///叉积  绝对值也是有向面积
}
bool pd(point a,point b,point c,point d) {
    double c1=cross(b-a,c-a);
    double c2=cross(b-a,d-a); /// c1*c2<=0  表示cd在ab左右,并不能表示ab在cd左右
    double c3=cross(d-c,a-c);
    double c4=cross(d-c,b-c); /// c3*c4<=0  表示ab在cd左右,并不能表示cd在ab左右
    return dcmp(c1)*dcmp(c2)<=0;   ///  cd在ab左右才返回true;
}
double intersection(point a,point b,point c,point d){
    double c1=cross(b-a,c-a);
    double c2=cross(b-a,d-a);
    if(dcmp(c1)*dcmp(c2)<0){
        /// x=(c2*c.x-c1*d.x)/(c2-c1);
        /// y=(c2*c.y-c1*d.y)/(c2-c1); 求直线ab与线段cd的交点坐标(前提是cd在ab的左右)
        return (c2*c.x-c1*d.x)/(c2-c1);
    }
    if(dcmp(c1)*dcmp(c2)==0){
        if(dcmp(c1)==0)
            return c.x;
        else return d.x;
    }
    return -maxn*1.0;
}
int main() {
    while(scanf("%d",&n)!=EOF) {
        if(n==0) break;
        for(int i=0; i<n; i++) {
            scanf("%lf%lf",&s[i].x,&s[i].y);
            p[i].x=s[i].x;
            p[i].y=s[i].y-1;
        }
        double ans=-maxn*1.0;
        int flag=0,k;
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                if(i!=j) {
                    for(k=0; k<n; k++) {
                        if(!pd(s[i],p[j],s[k],p[k])) {
                            break;
                        }
                    }
                    if(k>=n) {
                        flag=1;
                        break;
                    }
                    else if(k>=max(i,j)){
                        double temp=intersection(s[i],p[j],s[k],s[k-1]);
                        ans=max(temp,ans);
                        temp=intersection(s[i],p[j],p[k],p[k-1]);
                        ans=max(ans,temp);
                    }
                }
            }
            if(flag) break;
        }
        if(flag) printf("Through all the pipe.\n");
        else printf("%.2f\n",ans);
    }
    return 0;
}


你可能感兴趣的:(poj1039 几何)