SOJ 1041: Pipe

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 tex2html_wrap_inline31 , where tex2html_wrap_inline33 . 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 tex2html_wrap_inline37 there is a corresponding bottom point tex2html_wrap_inline39 (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 tex2html_wrap_inline43 and tex2html_wrap_inline45 (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 tex2html_wrap_inline47 on separate line. Each of the next n lines contains a pair of real values tex2html_wrap_inline51 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 tex2html_wrap_inline57 , 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.

思路:
这道题rj的黑书中做过介绍,这里用的是一种简单粗暴(效率较低)的方法:先枚举限制最长光线的拐点(易知限制点必然是一上一下,
应为如果两个限制点都在一侧必然可以围绕一个点旋转,做出更长的光线),如果作出的直线与最左侧的点对连成的线段相交那么这就是
一条合法枚举,从第二个拐点开始检查光线是否与上下两拐点的连线相交,知道找到与直线不相交的拐点连线,那么光线的终止点一定是
在这一点对与上一个点对(最后一个合法点对)之间的管道壁上,求出交点即可,维护所有光线终点的最大x坐标值,(不是x相对入口点
增量!!!),之如果检查所有拐点对连线都相交,那么这条光线可以射穿管道。

代码贴起:
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cfloat>
 4 #define eps 1e-7
 5 #define max(a,b) (a>b?a:b)
 6 
 7 int dcmp(double a){
 8     if (fabs(a)<eps)return 0;
 9     if (a>0)return 1;
10     return -1;
11 }
12 
13 typedef struct point{double x,y;}point;
14 
15 point mak(double x,double y){
16     point t;
17     t.x=x;t.y=y;
18     return t;
19 }
20 
21 point operator+(point a,point b){return mak(a.x+b.x,a.y+b.y);}
22 point operator-(point a,point b){return mak(a.x-b.x,a.y-b.y);}
23 point operator*(double a,point b){return mak(a*b.x,a*b.y);}
24 double operator^(point a,point b){return a.x*b.y-a.y*b.x;}
25 bool ifto(point a,point b,point c,point d){return ((a-b)^(c-b))*((a-b)^(d-b))<0;}
26 bool iftop(point a,point b,point c,point d){return ((a-b)^(c-b))*((a-b)^(d-b))<=0;}
27 point getp(point a,point b,point c,point d){
28     point t1=a-b,t2=c-d,t3=a-c;
29     return a+((t2^t3)/(t1^t2))*t1;
30 }
31 
32 point a[100];
33 
34 int main(){
35     int i,j,k,n,bo;
36     double ans;
37     point pl=mak(0,-1);
38     freopen("d:\\asdf.txt","r",stdin);
39     while(scanf("%d",&n)==1&&n){
40         for (i=1;i<=n;i++)scanf("%lf %lf",&a[i].x,&a[i].y);
41         ans=-DBL_MAX;bo=0;
42         for (i=1;i<=n;i++)
43             for (j=1;j<=n;j++)
44                 if (i!=j&&iftop(a[i],a[j]+pl,a[1],a[1]+pl)){
45                     for (k=1;k<n;k++)if (!iftop(a[i],a[j]+pl,a[k+1],a[k+1]+pl))break;
46                     if (k==n){bo=1;goto l;}
47                     else{
48                         ans=iftop(a[i],a[j]+pl,a[k],a[k+1])?max(ans,getp(a[i],a[j]+pl,a[k],a[k+1]).x):ans;
49                         ans=iftop(a[i],a[j]+pl,a[k]+pl,a[k+1]+pl)?max(ans,getp(a[i],a[j]+pl,a[k]+pl,a[k+1]+pl).x):ans;
50                     }
51                 }
52 l:if (bo)printf("Through all the pipe.\n");
53   else printf("%.2lf\n",ans);
54     }
55     return 0;
56 }

 

转载于:https://www.cnblogs.com/shellbase/archive/2013/05/08/3066846.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值