zoukankan      html  css  js  c++  java
  • POJ 1039 Pipe(直线相交)

    Pipe
    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

    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.

    //Memory  Time 
    //456K    63MS 
    
    #include<iostream>
    #include<cmath>
    #include<iomanip>
    using namespace std;
    
    const double precision=1e-3;   //精度限制
    const double inf=99999.0;   //正无穷,注意下面使用的是负无穷
    
    typedef class Node    //折点坐标
    {
    public:
    	double x;
    	double y;
    }point;
    
    int max(int a,int b)
    {
    	return a>b?a:b;
    }
    
    /*把浮点p的值转化为0,1或-1  (精度讨论)*/
    
    int dblcmp(double p)
    {
    	if(fabs(p)<precision)    // fabs()  浮点数的绝对值
    		return 0;       //只要是在0的邻域,就认为是0
    
    	return p>0?1:-1;
    }
    
    /*叉积运算*/
    
    double det(double x1,double y1,double x2,double y2)
    {
    	return x1*y2-x2*y1;
    }
    
    /*计算P点在AB的左侧还是右侧(AC与AB的螺旋关系)*/
    
    double cross(point A,point B,point P)
    {
    	return det(B.x-A.x , B.y-A.y , P.x-A.x , P.y-A.y);
    }
    
    /*判断直线AB、CD是否相交*/
    
    bool check(point A,point B,point C,point D)
    {
    	return (dblcmp(cross(A,B,C)) * dblcmp(cross(A,B,D)) <= 0);
    	//这里对黑书P353所述模板的相交约束条件做了修改
    	//目的是允许 入射光线L 与 折点处垂线 不规范相交(即垂线的端点可以落在L上 或者 允许延长线相交)
    }
    
    /*计算直线AB、CD的交点横坐标*/
    //本题这里传参是有讲究的,AB是代表光线L与管道的交点,CD是代表上管壁或者下管壁的端点
    //之所以这样做,是因为AB与CD实质上是不相交的,是AB的延长线与CD相交
    //按照上述传参顺序,根据修改后的模板,那么仅仅判断C、D是否在AB的两侧,就能计算 AB延长线与CD的交点
    //倘若传参顺序错了,就会判断A、B是否在CD的两侧,但是AB一定是在CD同侧的,也就不能求交点了
    
    double intersection(point A,point B,point C,point D)
    {
    	double area1=cross(A,B,C);
    	double area2=cross(A,B,D);
    	int c=dblcmp(area1);
    	int d=dblcmp(area2);
    
    	if(c*d<0) //CD在AB的两侧,规范相交
    		return (area2*C.x - area1*D.x)/(area2-area1);  //黑书P357交点计算公式
    
    	if(c*d==0)   //CD的其中一个端点在AB上,不规范相交
    		if(c==0)
    			return C.x;//C在AB上,返回AB与CD非规范相交时的交点C的横坐标
    		else
    			return D.x;//D在AB上,返回AB与CD非规范相交时的交点D的横坐标
    
    	return -inf;  //CD在AB同侧,无交点,返回 负无穷
    }
    
    int main(int i,int j,int k)
    {
    	int n;    //折点数
    	while(cin>>n)
    	{
    		if(!n)
    			break;
    
    		point* up=new point[n+1];         //上折点
    		point* down=new point[n+1];       //下折点
    
    		double max_x=-inf;  //最大可见度(管中最远可见点的横坐标)
    		/*Input*/
    
    		for(i=1;i<=n;i++)
    		{
    			cin>>up[i].x>>up[i].y;
    			down[i].x=up[i].x;
    			down[i].y=up[i].y-1;
    		}
    
    		bool flag=false;  //标记当前光线L(直线up[i]->down[j])能否贯通全管
    		for(i=1;i<=n;i++) //枚举所有通过一个上折点、一个下折点的直线
    		{
    			for(j=1;j<=n;j++)
    				if(i!=j)
    				{
    					for(k=1;k<=n;k++)     //直线L最大延伸到第k-1节管子
    						if(!check(up[i],down[j],up[k],down[k]))   //up[k]->down[k]为折点处垂直x轴的直线
    							break;
    
    					if(k>n)
    					{
    						flag=true;
    						break;
    					}
    					else if(k>max(i,j))  //由于不清楚L究竟是与第k-1节管子的上管壁还是下管壁相交,因此都计算交点,取最优
    					{                   //举例:若实际L是与上管壁相交,当计算下管壁时,得到的是第k-1个下折点,并不会是最优
    						                //反之亦同理 
    						double temp=intersection(up[i],down[j],up[k],up[k-1]);
    						if(max_x < temp)  //L与第k-1节管子的上管壁相交
    							max_x=temp;
    
    						temp=intersection(up[i],down[j],down[k],down[k-1]);
    						if(max_x < temp)  //L与第k-1节管子的上管壁相交
    							max_x=temp;
    					}
    				}
    
    			if(flag)
    				break;
    		}
    
    		if(flag)
    			cout<<"Through all the pipe."<<endl;
    		else
    			cout<<fixed<<setprecision(2)<<max_x<<endl;
    
    		/*Relax Room*/
    
    		delete up,down;
    	}
    	return 0;
    }





  • 相关阅读:
    轻松搞定技术面系列 1——基础篇
    Java集合排序(看完秒懂)
    详解 jupyter notebook 集成 spark 环境安装
    Java——观察者模式实例
    Java——泛型(最易懂的方式讲解泛型)
    Linux目录
    Python目录
    Nginx入门与实战
    数组与链表
    图算法之——dijkstra算法
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717932.html
Copyright © 2011-2022 走看看