poj 1039 Pipe(直线方程的应用)

Pipe
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6856   Accepted: 2009

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. 
poj 1039 Pipe(直线方程的应用)_第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

题目:http://poj.org/problem?id=1039

分析:这题涉及的计算几何知识,均是高中知识,对于点(x1,y1),(x2,y2)构成的直线方程ax+by+c=0,有a=y1-y2,b=x2-x1,c=x1*y2-x2*y1;

两直线的交点当然推算一下就出来了,这题只要枚举任意两个拐角口,选择两条对角的直线,判断两条直线是否能通过所有管道就行

注意,x的值会出现负的,所以一开始请把值负为无穷大

代码:

#include<cstdio>
#include<iostream>
using namespace std;
double x[22],y[22];
double a,b,c,ans;
int i,n;
void count(double x1,double y1,double x2,double y2)
{
    double a1=y1-y2,b1=x2-x1,c1=x1*y2-x2*y1;
    ans=max(ans,(b1*c-b*c1)/(a1*b-a*b1));
}
bool check(double x1,double y1,double x2,double y2)
{
    a=y1-y2,b=x2-x1,c=x1*y2-x2*y1;
    for(int i=1;i<=n;++i)
        if((a*x[i]+b*y[i]+c)*(a*x[i]+b*(y[i]-1)+c)>1e-8)
        {
            if(i>1)
            {
                count(x[i],y[i],x[i-1],y[i-1]);
                count(x[i],y[i]-1,x[i-1],y[i-1]-1);
            }
            return 0;
        }
    return 1;
}
bool ok()
{
    for(int i=1;i<n;++i)
        for(int j=i+1;j<=n;++j)
            if(check(x[i],y[i]-1,x[j],y[j])||check(x[i],y[i],x[j],y[j]-1))return 1;
    return 0;
}
int main()
{
    while(scanf("%d",&n),n)
    {
        for(i=1;i<=n;++i)
            scanf("%lf%lf",&x[i],&y[i]);
        ans=-1e50;
        if(ok())puts("Through all the pipe.");
        else printf("%.2lf\n",ans);
    }
    return 0;
}


你可能感兴趣的:(File,equals,input,each,output,pair)