原题链接:http://poj.org/problem?id=1039
博主的中文题面:
https://www.luogu.org/problemnew/show/T22993
Pipe
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<=20,就一定要用暴力呀,否则辜负了出题人一片苦心(滑稽)。于是,因为有上面的玄学结论,所以我们可以枚举所有的上下端点从而枚举出所有可能的直线,再从左到右依次检验光线能否通过,不断更新最远点即可O(n^3)水过。
细节
检验不必每次都计算光线与管壁的交点,只需要算光线与拐弯处的竖直直线的交点即可。因此在枚举时可以算出直线解析式,检验的时候代x进去判y就好了,当枚举到无法通过时再算光线与管壁的交点。
另外,POJ上可能出现G++ WA,C++ AC的情况(博主的都可以AC)。
代码
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define db double
using namespace std;
const db eps=1e-5;
struct pt{db x,y;};
pt operator - (pt a,pt b){pt r;r.x=a.x-b.x;r.y=a.y-b.y;return r;}
pt operator + (pt a,pt b){pt r;r.x=a.x+b.x;r.y=a.y+b.y;return r;}
pt operator * (pt a,db b){pt r;r.x=a.x*b;r.y=a.y*b;return r;}
pt operator / (pt a,db b){pt r;r.x=a.x/b;r.y=a.y/b;return r;}
int n;
db ans=-1e9;
pt pep[25];
void in()
{
for(int i=1;i<=n;++i)
scanf("%lf%lf",&pep[i].x,&pep[i].y);
}
int sig(db x){return (x>eps)-(x<-eps);}
db area(pt a,pt b,pt c)
{
db r=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
return r<0?-r:r;
}
db inter(db k,db b,db x1,db y1,db x2,db y2)
{
/*printf("%.2lf %.2lf\n",a1.x,a2.x);
printf("%d %d\n",p,num);
pt b1,b2;
b1=pep[num-1];b2=pep[num];
b1.y-=p;b2.y-=p;
int d1,d2;
d1=area(b1,a1,a2);
d2=area(b2,a1,a2);
return (b1-b2)*d2/(d1+d2)+b2;*/
return (x1*(y2-y1)+(x2-x1)*(b-y1))/(y2-y1-k*(x2-x1));
}
bool kk;
void check(db a1,db a2,db b1,db b2)
{
pt aa,bb;
aa.x=a1;aa.y=a2;bb.x=b1;bb.y=b2;
db k,b,h;
k=-1*(b2-a2)/(a1-b1);
b=-1*(a2*b1-b2*a1)/(a1-b1);
// b=a2-a1*k;
// printf("%.2lf %.2lf\n",k,b);
h=k*pep[1].x+b;
if(sig(pep[1].y-h)<0||sig(pep[1].y-1-h)>0){return;}
// printf("h:%.2lf\n",h);
for(int i=2;i<=n;++i)
{
h=k*pep[i].x+b;
// printf("h:%.2lf\n",h);
if(sig(h-pep[i].y)<=0&&sig(h-pep[i].y+1)>=0) continue;
db kk=(pep[i].y-pep[i-1].y)/(pep[i].x-pep[i-1].x);
if(sig(k-kk)>0)
{
ans=max(inter(k,b,pep[i-1].x,pep[i-1].y,pep[i].x,pep[i].y),ans);
return;
}
else
{
ans=max(inter(k,b,pep[i-1].x,pep[i-1].y-1,pep[i].x,pep[i].y-1),ans);
return;
}
// printf("ans:%.2lf\n",ans);
}
ans=pep[n].x;
}
void ac()
{
ans=pep[1].x;
if(n<=2)
{
printf("Through all the pipe.\n");
return;
}
for(int i=1;i<n;++i)
for(int j=i+1;j<=n;++j)
{
check(pep[i].x,pep[i].y,pep[j].x,pep[j].y-1);
check(pep[i].x,pep[i].y-1,pep[j].x,pep[j].y);
}
if(sig(ans-pep[n].x)==0) printf("Through all the pipe.\n");
else printf("%.2lf\n",ans);
}
void reset()
{
kk=0;ans=-1e9;
memset(pep,0,sizeof(pep));
}
int main()
{
while(scanf("%d",&n)&&n)
{
reset();in();ac();
}
return 0;
}