Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10281 | Accepted: 3179 |
Description
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
Output
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 <iostream>
#include<cstring>
#include <algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const double eps=1e-4;
const double inf=9999999;
struct Point
{
double x,y;
Point (double a=0,double b=0):x(a),y(b) {}
};
int Max(int a,int b)
{
return a>b?a:b;
}
int dblcmp(double p)
{
if(fabs(p)<eps)
return 0;
return p>0?1:-1;
}
double multiply(Point sp,Point ep,Point op)
{
return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
}
bool check(Point A,Point B,Point C,Point D)
{
return (dblcmp(multiply(A,B,C)) * dblcmp(multiply(A,B,D)) <= 0);
}
double intersection(Point A,Point B,Point C,Point D)
{
double area1=multiply(A,B,C);
double area2=multiply(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)
if(c==0)
return C.x;
else
return D.x;
return -inf;
}
int main()
{
int n,i,j,k;
Point up[21],down[21];
while(~scanf("%d",&n))
{
if(n<=0) break;
bool Flag=false;
for(i=0; i<n; i++)
{
scanf("%lf %lf",&up[i].x,&up[i].y);
down[i].x=up[i].x;
down[i].y=up[i].y-1;
}
double max_x=-inf;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(i==j) continue;
for(k=0; k<n; k++)
if(!check(up[i],down[j],up[k],down[k]))
break;
if(k>n-1)
{
Flag=true;
break;
}
else if(k>Max(i,j))
{
double temp=intersection(up[i],down[j],up[k],up[k-1]);
if(max_x < temp)
max_x=temp;
temp=intersection(up[i],down[j],down[k],down[k-1]);
if(max_x < temp)
max_x=temp;
}
}
if(Flag)
break;
}
if(Flag)
printf("Through all the pipe.\n");
else
printf("%.2lf\n",max_x);
}
return 0;
}