# poj1039 Pipe

Pipe
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11603 Accepted: 3633

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.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#define eps 1e-7//控制精度
#define inf 0x3f3f3f3f//设置最大值
using namespace std;
const double precision=1e-6;//黑书上的控制精度
struct Point
{
double x;
double y;
}up[100000],under[100000];//上下顶点坐标
int dblcmp(double d)//黑书上的小技巧，返回整数值，便于计算和判断
{
if(fabs(d)<precision)
return 0;
return (d>0)?1:-1;
}
double det(double x1,double y1,double x2,double y2)//计算叉积
{
return x1*y2-x2*y1;//叉积公式
}
double cross(Point a , Point b , Point c)//哪些点需要计算
{
return det(b.x-a.x , b.y-a.y , c.x-a.x , c.y-a.y);
}
bool check(Point A,Point B,Point C,Point D)//看的网上大牛的优化
{
return (dblcmp(cross(A,B,C))*dblcmp(cross(A,B,D))<=0);//一会贴上大牛的链接
}
double fax(Point A,Point B,Point C,Point D)//判断交点
{
double Area1=cross(A,B,C);//就是这个地方，利用叉积和定比分点求交点搞得我很焦灼啊
double Area2=cross(A,B,D);
int c=dblcmp(Area1);
int d=dblcmp(Area2);//这些都是黑书上的板子
if(c*d<0)//规范相交
return (Area2*C.x-Area1*D.x)/(Area2-Area1);
if(c*d==0)//CD的一个顶点在AB上，不规范相交
{
if(c==0)//C在AB上
return C.x;
else//D在AB上
return D.x;
}
return -inf;//无交点
}
int main()
{
int n,i;
while(~scanf("%d",&n)&&n)
{
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&up[i].x,&up[i].y);//输入坐标
under[i].x=up[i].x;
under[i].y=up[i].y-1;
}
bool flag=false;//判断是否贯穿
int j,k;
double maxc=-inf;
for(i=1;i<=n;i++)//开始暴力
{
for(j=1;j<=n;j++)
{
if(i==j)
continue;
for(k=1;k<=n;k++)//直线最大延伸到第k-1条管子
{
if(!check(up[i] , under[j] , up[k] , under[k]))//仔细想想这里就会明白这里是正确的了
break;
}
if(k>n)//已贯穿
{
flag=true;
break;
}
else if(k>max(i,j))//判断是上顶点还是下顶点
{//举例：若直线与上顶点相交，计算下顶点时，得到的是第k-1个下顶点（下折点），并不一定是最优。
double temp=fax(up[i] , under[j] , up[k] , up[k-1]);//直线与上顶点相交
if(maxc<temp)
maxc=temp;
temp=fax(up[i] , under[j] , under[k] , under[k-1]);//直线与下顶点相交
if(maxc<temp)
maxc=temp;
}
}
if(flag)
break;
}
if(flag)
printf("Through all the pipe.\n");//贯穿
else
printf("%.2f\n",maxc);
}
return 0;
}  

• 评论

• 上一篇
• 下一篇