这题做了整整一下午,被折磨得死去活来。
思路是枚举两个端点,然后如果跟管道拐口的线都有交点则可以穿过整个管道,我一开始枚举端点,然后跟拐口的线比,然后如果没有交点,那么就计算x的值,但是这样会有一个问题,那就是如果枚举的两个端点前面有线没有相交,那么他会计算x的值,导致x的值变小了,所以在判断点前的线时,单独判断就好了。
//
// main.cpp
// Richard
//
// Created by 邵金杰 on 16/8/8.
// Copyright © 2016年 邵金杰. All rights reserved.
//
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
#define INF 1e20
#define EPS 1e-6
struct Point{
double x,y;
Point(double x,double y): x(x),y(y) {}
};
struct Vector{
double x,y;
Vector(double x,double y): x(x),y(y) {}
};
struct Line{
Point a,b;
Line(Point a,Point b): a(a),b(b) {}
};
Vector operator - (Point p,Point q)
{
return Vector(p.x-q.x,p.y-q.y);
}
Vector operator * (double k,Vector p)
{
return Vector(k*p.x,k*p.y);
}
Point operator + (Point p,Vector q)
{
return Point(p.x+q.x,p.y+q.y);
}
double getX(double y,Point a,Point b)
{
return a.x+(b.x-a.x)*((y-a.y)/(b.y-a.y));
}
vector<Point> ve;
vector<Line> le;
double operator ^ (Vector p,Vector q)
{
return p.x*q.y-p.y*q.x;
}
double cross(Vector p,Vector q)
{
return p.x*q.y-p.y*q.x;
}
bool IsZero(double x)
{
return -EPS<x&&x<EPS;
}
double intersect(Line l,Line m)
{
double x=cross(l.a-m.a,m.b-m.a)/2;
double y=cross(m.b-m.a,l.b-m.a)/2;
Point p=l.a+(x/(x+y))*(l.b-l.a);
return p.x;
}
void work()
{
double Max=-INF;
int lenv=(int)ve.size();
int lenl=(int)le.size();
for(int i=0;i<lenv;i++)
{
int s;
if(i%2==0) s=i+2;
else s=i+1;
for(int j=s;j<lenv;j++)
{
bool success=true;
int p=i/2;
for(int k=0;k<p;k++)
{
if(cross(le[k].a-ve[i],ve[j]-ve[i])*cross(le[k].b-ve[i],ve[j]-ve[i])>EPS)
{
success=false;
break;
}
}
if(!success) continue;
bool ok=true;
for(int k=p;k<lenl;k++)
{
if(cross(le[k].a-ve[i],ve[j]-ve[i])*cross(le[k].b-ve[i],ve[j]-ve[i])>EPS)
{
ok=false;
if(cross(le[k].a-ve[i],ve[j]-ve[i])*cross(le[k-1].a-ve[i],ve[j]-ve[i])<=-EPS){
Max=max(Max,intersect(Line(le[k].a,le[k-1].a),Line(ve[j],ve[i])));
}
else if(cross(le[k].b-ve[i],ve[j]-ve[i])*cross(le[k-1].b-ve[i],ve[j]-ve[i])<=-EPS){
Max=max(Max,intersect(Line(le[k].b,le[k-1].b),Line(ve[j],ve[i])));
}
break;
}
}
if(ok) {printf("Through all the pipe.\n"); return ;}
}
}
printf("%.2f\n",Max);
}
int main()
{
int n;
double x,y;
while(scanf("%d",&n)&&n)
{
ve.clear();
le.clear();
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&x,&y);
ve.push_back(Point(x,y));
ve.push_back(Point(x,y-1));
le.push_back(Line(Point(x,y),Point(x,y-1)));
}
if(n<3) printf("Through all the pipe.\n");
else work();
}
return 0;
}