poj_1039 Pipe(直线与线段相交)

来源:互联网 发布:python partial 编辑:程序博客网 时间:2024/05/16 13:55
Pipe
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10518 Accepted: 3261

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

40 12 24 16 460 12 -0.65 -4.457 -5.5712 -10.817 -16.550

Sample Output

4.67Through all the pipe.
数据很强的一道题,一开始的思路是去判断 每个枚举的两个顶点形成的光线 与 一路上经过的上管壁和下管壁 是否相交。
使用这种思路要多去判断管壁拐向问题,也就是当光线与管壁端点相交时,有可能光线还能继续延伸,或者直接被管壁档住,
这取决于光线与下一段管壁的位置关系,可以用叉积去判断。这样产生了很多细节问题,比如可能要对第一段管壁特判。
细节没考虑清楚很难过。其实还有一种简单的思路,就是不去判断光线是否与管壁相交,只需判断光线是否不能通过拐角。
在程序中表现为光线是否与某一对x相同的上下顶点连成的线段相交,如果相交,则能通过。之后只需判断不能通过的拐角的位置
是否在形成光线的两个顶点之后,如果是,那光线与该拐角的前一段管壁一定相交。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Point{    double x, y;    Point(double x = 0, double y = 0) : x(x), y(y) { }};typedef Point Vector;Vector operator + (Vector A, Vector B){    return Vector(A.x + B.x, A.y + B.y);}Vector operator - (Point A, Point B){    return Vector(A.x - B.x, A.y - B.y);}Vector operator * (Vector A, double p){    return Vector(A.x * p, A.y * p);}Vector operator / (Vector A, double p){    return Vector(A.x / p, A.y / p);}const double eps = 1e-10;int dcmp(double x){    if(fabs(x) < eps) return 0;    else return x < 0 ? -1 : 1;}bool operator == (const Point &a, const Point &b){    return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;}double Dot(Vector A, Vector B){    return A.x*B.x + A.y*B.y;}double Length(Vector A){    return sqrt(Dot(A, A));}double Cross(Vector A, Vector B){    return A.x*B.y - A.y*B.x;}//判断直线a1a2与线段b1b2是否相交,包括不规范相交bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){    double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);    if(dcmp(c1) * dcmp(c2) <= 0) return 1;    else return 0;}Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){    Vector u = P - Q;    double t = Cross(w, u) / Cross(v, w);    Point ans = P + v * t;    return P + v * t;}Point read_point(){    double x, y;    scanf("%lf%lf", &x, &y);    return Point(x, y);}int n;Point top[22], down[22];int main(){    while(~scanf("%d", &n) && n)    {        for(int i = 1; i <= n; i++) top[i] = read_point(), down[i] = Point(top[i].x, top[i].y-1);        bool flag = 0;        double ans = top[1].x;        for(int i = 1; i <= n; i++)        {            for(int j = 1, k; j <= n; j++)            {                if(i == j) continue;                for(k = 1; k <= n; k++)                {                    if(!SegmentProperIntersection(top[i], down[j], top[k], down[k])) break;                }                if(k > n){ flag = 1; break; }                if(k > max(i, j))                {                    Point t = GetLineIntersection(top[i], down[j]-top[i], top[k-1], top[k]-top[k-1]);                    if(t.x > ans) ans = t.x;                    t = GetLineIntersection(top[i], down[j]-top[i], down[k-1], down[k]-down[k-1]);                    if(t.x > ans) ans = t.x;                }            }            if(flag) break;        }        if(flag) printf("Through all the pipe.\n");        else printf("%.2f\n", ans);    }    return 0;}


0 0