判断直线相交思维好题 (poj1039)

版权声明:转载什么的无所谓啦,反正注明一下出处就行啦~ https://blog.csdn.net/u013672056/article/details/76220118

Pipe

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10951 Accepted: 3405

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.


解题思路。

由于n非常的小,于是我们可以采用暴力的做法。

我们可以通过观察可以的出,如果一条直线从管道射入,其实等价于上曲>线和下曲线顶点的连线(我们可以通过平移证明它)

得出这一点之后我们可以枚举所有的上端点以及下端点设为$ (a,b) \(, 然后通过这一条线,从后面找出他的交后面的边的点。 然后我们枚举后继节点我们可以求出上面的线和下面的线和\) line(a,b) $ 的交点然后求出$ x $ 就行了。
但是我们在枚举后继的时候并不是每一种情况都符合的。我们可以利用差积判断在$ line(a,b) $ 的两端还是否,这样我们就可以求出这种情况是否满足了。
当然如果枚举到了$ n $ 还都满足的话我们那么他就可以穿过所有的管道。

补充一下,为什么只要枚举到了n就不管后面了。
因为如果前面的 $ line(a,b) $ 不满足,后面的也一定不满足
我们可以转换一下到后面的管道的线,就像之前转换成上端点和下端点>一样,这样就可以保证$ true $了

#pragma GCC optimize("O2")
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
using namespace std;
const double eps=1e-9;
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y){x=_x,y=_y;}
};
typedef Point Vector ;
Vector operator - (Point a,Point b){return Point(a.x-b.x,a.y-b.y);}
int dcmp(double x){if(fabs(x)<eps) return 0;else return x>0?1:-1;}
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}
Point line_intersection(Point p1,Point p2,Point p3,Point p4)
{
    double a1=p1.y-p2.y,b1=p2.x-p1.x,c1=Cross(p1,p2);
    double a2=p3.y-p4.y,b2=p4.x-p3.x,c2=Cross(p3,p4);
    return Point((c1*b2-c2*b1)/(a2*b1-a1*b2),(a2*c1-a1*c2)/(a1*b2-a2*b1));
}
Point a[200],b[200];int n;

int main()
{
    while(cin>>n&&n)
    {
        double ans=0;bool ok=0;
        for(int i=1;i<=n;i++)
            scanf("%lf%lf",&a[i].x,&a[i].y),b[i]=Point(a[i].x,a[i].y-1);
        ans=a[1].x;
        for(int i=1;i<=n&&!ok;i++)
            for(int j=1;j<=n&&!ok;j++) if(i!=j)
            {
                for(int k=1;k<=n&&!ok;k++)
                {
                    if(dcmp(Cross(a[k]-a[i],b[j]-a[i])*Cross(b[k]-a[i],b[j]-a[i]))==1)
                    {
                        if(k>i&&k>j)
                        {
                            ans=max(ans,line_intersection(a[i],b[j],a[k-1],a[k]).x);
                            ans=max(ans,line_intersection(a[i],b[j],b[k-1],b[k]).x);
                        }
                        break;
                    }
                    if(k==n) ans=1e13,ok=1; 
                }
            }
        !dcmp(ans-1e13)?puts("Through all the pipe."):printf("%.2f\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u013672056/article/details/76220118