SOJ 1041: Pipe
2013-05-08 15:09
411 查看
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
, where
. 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
there is a corresponding bottom point
(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
and
(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.
on separate line. Each of the next n lines contains a pair of real values
separated by space. The last block is denoted with n = 0.
, then the message Through all the pipe. will appear in the output file.
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
, where
. 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
there is a corresponding bottom point
(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
and
(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 pointson separate line. Each of the next n lines contains a pair of real values
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, 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相对入口点 增量!!!),之如果检查所有拐点对连线都相交,那么这条光线可以射穿管道。 代码贴起:
#include <cstdio> #include <cmath> #include <cfloat> #define eps 1e-7 #define max(a,b) (a>b?a:b) int dcmp(double a){ if (fabs(a)<eps)return 0; if (a>0)return 1; return -1; } typedef struct point{double x,y;}point; point mak(double x,double y){ point t; t.x=x;t.y=y; return t; } point operator+(point a,point b){return mak(a.x+b.x,a.y+b.y);} point operator-(point a,point b){return mak(a.x-b.x,a.y-b.y);} point operator*(double a,point b){return mak(a*b.x,a*b.y);} double operator^(point a,point b){return a.x*b.y-a.y*b.x;} bool ifto(point a,point b,point c,point d){return ((a-b)^(c-b))*((a-b)^(d-b))<0;} bool iftop(point a,point b,point c,point d){return ((a-b)^(c-b))*((a-b)^(d-b))<=0;} point getp(point a,point b,point c,point d){ point t1=a-b,t2=c-d,t3=a-c; return a+((t2^t3)/(t1^t2))*t1; } point a[100]; int main(){ int i,j,k,n,bo; double ans; point pl=mak(0,-1); freopen("d:\\asdf.txt","r",stdin); while(scanf("%d",&n)==1&&n){ for (i=1;i<=n;i++)scanf("%lf %lf",&a[i].x,&a[i].y); ans=-DBL_MAX;bo=0; for (i=1;i<=n;i++) for (j=1;j<=n;j++) if (i!=j&&iftop(a[i],a[j]+pl,a[1],a[1]+pl)){ for (k=1;k<n;k++)if (!iftop(a[i],a[j]+pl,a[k+1],a[k+1]+pl))break; if (k==n){bo=1;goto l;} else{ 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; 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; } } l:if (bo)printf("Through all the pipe.\n"); else printf("%.2lf\n",ans); } return 0; }
相关文章推荐
- SOJ 1041
- W-Pipe
- EasyNetQ使用(十)【用EasyNetQ.Hosepipe重新提交错误信息】
- docker+pipework配置容器网络(二)
- PAT 1041. 考试座位号(15)
- linux 下 tomcat 运行报错 Broken pipe
- 利用pipe执行远程机器命令的分析
- ipc - demo for pipe
- (转)关于pipe()的详细解析
- A Unix Utility You Should Know About: Pipe Viewer
- 系统程序员成长计划-管道过滤器(Pipe-And-Filter)模式
- 解决linux下webloigc经常出现broken pipe,socket....connection reset错误
- 学习笔记-进程间通信(Interprocess communication,IPC) --概念、 pipe and FIFOs
- Broken pipe解决办法
- pipe 函数 (C语言)
- Linux学习笔记——管道PIPE
- Pipe——Python 的中缀语法库
- 管線命令 (pipe)
- Shell 中的 Pipe 破裂机制浅谈
- pipe 函数 (C语言)