您的位置:首页 > 其它

JOJ1076 && POJ1039 Pipe 经典计算几何

2011-04-15 20:02 435 查看



1076: Pipe

ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE

3s8192K7522Standard
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 withy-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.


这是一道经典的求直线与线段相交的计算几何,在Lrj的黑书上有比较详细的描述。不过黑书里面的思想可取,做法效率不高。

首先,引用一段黑书上的分析:看到此题,我们应该能想到,如果一根光线自始自终未曾擦到任何的顶点,则这种情况下肯定不是最优的,

可以通过平移使之优化。其次,如果只碰到一个顶点,那也不是最优的,可以通过旋转,使它碰到另一个顶点,并且更优。

由此得知,一个最优的光线一定经过一个管道的上顶点和下顶点。

而按照黑书上的方法,是枚举管道中的一个上顶点和下顶点,然后求过这两点的直线所能交到的最远x。

不过我们可以想到另外一个比较好的方法,我们可以这样考虑,如果一条直线能穿过管道,根据上面的分析,

则可知这条直线一定经过第n个位置的上顶点或者下顶点,否则,一定与某个线段 AB, A(xi,yi),B(xi+1,yi+1)相交一点,

或者与平行AB的线段CD相交一点。

此时,我们不妨枚举光线经过的一个点,然后通过斜率确定一条经过某一个另一位置点的直线,然后通过斜率来判断是否可行。

下面贴出在代码

#include<iostream>
using namespace std;
const double eps = 1e-8;
struct Point{double x,y;}pnt[21];
int n;
/*
double calc_x(const Point &p,const Point &A,const Point &B,double k,bool cmd)
//求过p点斜率为k的直线与线段AB的交点的x
{
double kt = (B.y-A.y)/(B.x-A.x);
//cout << ((cmd?B.y-1:B.y)-p.y+k*p.x-kt*B.x)/(k-kt) << endl;
return ((cmd?B.y-1:B.y)-p.y+k*p.x-kt*B.x)/(k-kt);
}
*/

double calc_x(double x0,double y0,double x1,double y1,double xp,double yp,double k)
{
double kt = (y1-y0)/(x1-x0);
//printf("%.2lf/n",(y0-yp+k*xp-kt*x0)/(k-kt));
return (y0-yp+k*xp-kt*x0)/(k-kt);
}

double aimt(const Point &p0, bool cmd)
{
double ansk1 = -1e100, ansk2 = 1e100,
x0 = p0.x, y0 = cmd ? p0.y - 1 : p0.y;
Point tp = {x0,y0};
for(int i = 0; i < n; ++ i)
{
if(pnt[i].x == x0) continue;
double k1 = (pnt[i].y - y0 - 1)/(pnt[i].x - x0),
k2 = (pnt[i].y - y0)/(pnt[i].x - x0);
if(k1 > k2)
{
if(k1 < ansk1 || k2 > ansk2) return pnt[0].x;
ansk2 = k1 < ansk2 ? k1 : ansk2;
ansk1 = k2 > ansk1 ? k2 : ansk1;
}
else
{
//if(k1 > ansk2) return calc_x(tp,pnt[i],pnt[i-1],1,ansk2);
//if(k2 < ansk1) return calc_x(tp,pnt[i],pnt[i-1],0,ansk1);
if(k1 > ansk2) return calc_x(pnt[i].x, pnt[i].y-1, pnt[i-1].x, pnt[i-1].y-1, x0,y0,ansk2);
if(k2 < ansk1) return calc_x(pnt[i].x, pnt[i].y,   pnt[i-1].x, pnt[i-1].y,   x0,y0,ansk1);
ansk1 = k1 > ansk1 ? k1 : ansk1;
ansk2 = k2 < ansk2 ? k2 : ansk2;
}
}
return pnt[n-1].x;
}
int main()
{
while(scanf("%d",&n) && n)
{
for(int i = 0; i < n; ++ i)
scanf("%lf%lf",&pnt[i].x, &pnt[i].y);
double tp,rec = pnt[0].x;
for(int i = 0; i < n; ++ i)
{
if((tp = aimt(pnt[i],0)) > rec) rec = tp;
if(rec == pnt[n-1].x) break;
if((tp = aimt(pnt[i],1)) > rec) rec = tp;
if(rec == pnt[n-1].x) break;
}

if(rec == pnt[n-1].x) printf("Through all the pipe./n");
else printf("%.2lf/n",rec);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: