poj1039 Pipe

Pipe
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10234 Accepted: 3161

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.

大致题意:

有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道全部都是不透明的,不能够反射的,光线从左边入口处的(x1,y1),(x1,y1-1)之间射入,只能直线传播,求解光线最远能传播到哪里(取x坐标)或者是否能穿透整个管道.

思路:

恩,据说刘汝佳的黑书《算法艺术与信息学艺术》第三章 计算几何初步 的例2  P359有讲,可是博主表示并没有啊,好在有人写的比较详细,第一次遇见这样的题,表示,不看板子根本不会做,于是看了板子自己敲的,表示还是回味无穷。

能够到达最远点的直线必然通过管道的一个上管道壁的折点和一个下管道壁的折点,枚举所有的这样的折点,求出最远能够到达的地方的横坐标.




#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=20+5;
const double eps=1e-3;
const double inf=0x3f3f3f*1.0;
struct point
{
    double x,y;
} up[MAXN],down[MAXN];
//做计算几何的题,一定要考虑精度问题
int cmp(double x)//这个函数就相当于把叉积转化为符号0,-1,+1
{
    if(fabs(x)<eps)return 0;//在0的邻域内就是0
    return x>0.0?1:-1;
}
//叉积计算公式,这地方两个分开了一下
double chaji(double x1,double y1,double x2,double y2)
{
    return x1*y2-x2*y1;
}
double cross(point A,point B,point C)
{
    return chaji(B.x-A.x,B.y-A.y,C.x-A.x,C.y-A.y);
}
//这个是检查C、D两点和直线A、B的位置关系的
//如果AB×AC和AB×AD同号说明肯定CD两点在AB的同侧,异号说明就是在两侧也就是规范相交
//不懂得继续好好学习叉积定义去,可以参考我上一篇博客
//等于0说明共线,这个就是不规范相交,题目要求上也是不规范相交也是可以的
bool check(point A,point B,point C,point D)
{
    return (cmp(cross(A,B,C)*cmp(cross(A,B,D)))<=0);
}
//求AB和CD交点的公式,表示没有黑书QAQ,读者可以自己看看,公式就是应该记住的嘛
double jiaodian(point A,point B,point C,point D)
{
    double area1=cross(A,B,C);
    double area2=cross(A,B,D);
    int x1=cmp(area1);
    int x2=cmp(area2);
    if(x1*x2<0)//规范相交
        return (area2*C.x-area1*D.x)/(area2-area1);
    if(x1*x2==0)//不规范相交
    {
        if(!x1)return C.x;
        else return D.x;
    }
    return -inf;//没什么卵用其实
}

int main()
{
    int n;
    int i,j,k;
    while(~scanf("%d",&n)&&n)
    {
        double maxx=-inf;
        for(i=0; i<n; ++i)
        {
            scanf("%lf%lf",&up[i].x,&up[i].y);
            down[i].x=up[i].x;
            down[i].y=up[i].y-1;
        }
        int flag=0;
        for(i=0; i<n&&!flag; ++i)
            for(j=0; j<n; ++j)if(i!=j)
                {
                    for(k=0; k<n; ++k)if(!check(up[i],down[j],up[k],down[k]))break;//如果没能穿过,说明撞墙了
                    if(k==n)//如过都能穿过
                    {
                        flag=1;
                        break;
                    }

                    if(k>max(i,j))//如果直线合法,表示是撞墙了,更新最远点
                    {
                        double temp=jiaodian(up[i],down[j],up[k],up[k-1]);//撞了上面的墙
                        maxx=max(maxx,temp);
                        temp=jiaodian(up[i],down[j],down[k],down[k-1]);//撞了下面的
                        maxx=max(maxx,temp);
                    }
                }
        if(flag)printf("Through all the pipe.\n");
        else printf("%.2f\n",maxx);
    }
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值