您的位置:首页 > 其它

poj 1039 Pipe (线段相交判定)

2017-01-02 10:49 357 查看
Pipe

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10624 Accepted: 3287
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.

Source

Central Europe 1995
[Submit]   [Go Back]   [Status]  
[Discuss]

题目大意:给你一条不透光不反射的管子,从管子的最左端入射(上下端点之间的任意位置)一条光线,求最远能照射到x的坐标

题解:线段相交判定。

最远能照射到的位置,那么这条射线一定经过了管子的两个拐点,并且拐点一上一下。

那么我们可以枚举两个拐点,然后判断是否满足条件。如果射线能顺利的通过某个拐点处,那么该拐点的上下两个端点之间的线段一定与射线有交点。我们可以根据这个判断最远能到达那个拐点,如果该拐点在枚举的两个拐点较远的拐点之前那么就说明这是一个不合法的方案。如果在之后就求一下射线与管道壁的交点,更新答案。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 1000
#define eps 1e-3
#define inf 10000000
using namespace std;
struct vector{
double x,y;
vector (double X=0,double Y=0) {
x=X,y=Y;
}
}a
,b
;
int n;
double temp;
typedef vector point;
vector operator -(vector a,vector b){
return vector (a.x-b.x,a.y-b.y);
}
vector operator +(vector a,vector b){
return vector (a.x+b.x,a.y+b.y);
}
vector operator *(vector a,double t){
return vector (a.x*t,a.y*t);
}
int dcmp(double x)
{
if (fabs(x)<eps) return 0;
return x<0?-1:1;
}
double cross(vector a,vector b)
{
return a.x*b.y-a.y*b.x;
}
bool check(point a1,point a2,point b1,point b2)
{
double c1=cross(b2-b1,a1-b1),c2=cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<=0;
}
double calc(point a1,point a2,point b1,point b2)
{
vector v=a2-a1; vector w=b2-b1;
vector u=a1-b1;
if (dcmp(cross(v,w))==0) return 100000000;
double t=cross(w,u)/cross(v,w);
point now=a1+v*t;
return now.x;
}
int main()
{
freopen("a.in","r",stdin);
//freopen("my.out","w",stdout);
while (true) {
scanf("%d",&n);
if (!n) break;
for (int i=1;i<=n;i++)
scanf("%lf%lf",&a[i].x,&a[i].y),
b[i].x=a[i].x,b[i].y=a[i].y-1;
bool mark=false; double mx=-inf;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++){
if (i==j) continue;
int k=0;
for (k=1;k<=n;k++)
if (!check(a[k],b[k],a[i],b[j]))
break;
if (k>n) {
mark=true;
break;
}
double ans=inf;
if (k>max(i,j)) {
if (check(a[k],a[k-1],a[i],b[j])&&k-1!=i)
ans=min(ans,calc(a[k],a[k-1],a[i],b[j]));
if (check(b[k],b[k-1],a[i],b[j])&&k-1!=j)
ans=min(ans,calc(b[k],b[k-1],a[i],b[j]));
if (ans!=inf) mx=max(ans,mx);
}
}
if (mark) printf("Through all the pipe.\n");
else printf("%0.2lf\n",mx);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: