POJ1039 Pipe

这道题比较简单,就是不要吧上图中第一种情况漏判就可以了。另外最长的路线一点是由一个上方的点,和下方的点组成的直线。只要枚举每一种情况,再从第一个管道开始判断是否存在交点,如果存在则和当前的最大值,比较即可
Pipe
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7326 Accepted: 2131

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.
#include<iostream>
#include<cmath>
#include<cstdio>
#include<complex>
#include<cstring>
#include<string>
#include<iomanip>
#include<cstdlib>


using namespace std;
#define max(a,b)  (a>b?a:b)
#define min(a,b)  (a<b?a:b)
#define eps 1e-8
#define zero(x)   (((x)>0?(x):-(x))<eps)

struct  point{ double x,y; };
struct  line { point a,b ; };

#define MAXN 30

point up[MAXN],down[MAXN]; 
int n;

double xmult(point &a,point &b,point &c)  
{
	return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y); 
}

int sig(double k)  
{  
	if(fabs(k)<eps)
		return 0;
	return (k>0)?1:-1;  
}  

bool intersect(point a,point b,point c,point d)   //管口判断 
{
	double d1,d2;
	d1=xmult(a,b,c);
	d2=xmult(a,b,d);
	if(d1*d2<=0)
		return true;
	return false;                 
}
bool intersect2(point a,point b,point c,point d) //管壁判断 
{
	double d1,d2;
	d1=xmult(a,b,c);
	d2=xmult(a,b,d);
	if(d1*d2>=0)
		return false;
	return true;
}
point intersection(point a,point b ,point c, point d)         //求两条直线的交点 
{
	point temp=a;
	double t=((a.x-c.x)*(c.y-d.y)-(a.y-c.y)*(c.x-d.x))
			 /((a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x));
	temp.x+=(b.x-a.x)*t;
	temp.y+=(b.y-a.y)*t;
	return temp;
}

void init()
{
	for(int i=0;i<n;i++)
	{
		cin>>up[i].x>>up[i].y;
		down[i].x=up[i].x;
		down[i].y=up[i].y-1;
	}
}

void solve()   //判断能否进入一个管口时,直线与线段的相交要包括端点,而判断直线与管壁是否相交时,不能包括端点。 
{
	int k;
	double d1,d2;
	double x=up[0].x;
	point temp;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(!intersect(up[i],down[j],up[0],down[0]))   // 判断是否能进入第一个管口,否则直接进行下次循环 
				continue;
			for(k=1;k<n;k++)
			{
				d1=sig(xmult(up[i],down[j],up[k]));
				d2=sig(xmult(up[i],down[j],down[k]));
				if(d1*d2>0)                             //判断是否能够进入第k个管口,如果不能则交点一定在k-1,k之间 ,当能够进入一个管口时 
				{                                       //上下两个端点在直线的两侧,或者期中有一点在直线上。(注:如果有一点在直线上也有可能不能进入下一个管道,这种情况会在k+1次循环中排除) 
					if(intersect2(up[i],down[j],up[k],up[k-1]))   //交点在上线段 
					{
						temp=intersection(up[i],down[j],up[k],up[k-1]);
						x=max(temp.x,x);
						break;
					}
					if(intersect2(up[i],down[j],down[k],down[k-1])) //交点在下线段 
					{
						temp=intersection(up[i],down[j],down[k],down[k-1]);
						x=max(temp.x,x);
						break;
					}
					x=max(x,up[k-1].x);  //既不在上面也不再下面那么就是先一个入口的端点 ,就是上一次循环残留下来的情况,这里判断 
					break;
				}
			}
			if(k==n)
				x=up[n-1].x+1;		
		}
	}
	if(x>=up[n-1].x)
		cout<<"Through all the pipe."<<endl;
	else
		cout<<setprecision(2)<<setiosflags(ios::fixed)<<x<<endl;
}


int main()
{
	while(cin>>n && n)
	{
		init();
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值