poj 1039 Pipe 计算几何
Pipe
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 10281 | Accepted: 3179 |
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.

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 <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; }

本文详细解析了POJ1039 Pipe计算几何问题,介绍了如何通过计算几何的方法来确定光线在管道内部能够到达的最远位置。文章提供了完整的算法思路及代码实现。
701

被折叠的 条评论
为什么被折叠?



