Pipe
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9493   Accepted: 2877

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

题目大意:给出一个管道,让一束光穿过,找出一个穿过距离最长的,然后求出这个穿过距离最长的与管壁的交点的x坐标,如果可以完整的穿过整个管道,输出Through all the pipe.
思路: 枚举光线是否可以穿过每一个拐点官腔,也就是拐点所在位置的竖直线段,如果不能穿过说明它肯定与管壁相交,计算交点,如果都能相交,这时就是可以穿过整个管道。
注:刚开始枚举的时候枚举的特别麻烦,是直接枚举光线方向,从入口一次加0.01的枚举,判断特殊位置的时候特别麻烦
/*************************************************************************
> File Name: poj_1039.cpp
> Author: Howe_Young
> Mail: 1013410795@qq.com
> Created Time: 2015年05月01日 星期五 09时43分46秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define EPS 1e-8
#define INF 1e6
using namespace std;
struct point{
double x, y;
};
const int maxn = ;
point p[maxn];
int n;
int sgn(double x)
{
if (fabs(x) < EPS)
return ;
return x < ? - : ;
}
double x_multi(point p1, point p2, point p3)
{
return (p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y);
}
void get_intersection(point p1, point p2, point p3, point p4, double &x, double &y)
{
double a1, b1, c1, a2, b2, c2;//求交点过程
a1 = (p2.y - p1.y) * 1.0;
b1 = (p1.x - p2.x) * 1.0;
c1 = (p2.x * p1.y - p1.x * p2.y) * 1.0;
a2 = (p4.y - p3.y) * 1.0;
b2 = (p3.x - p4.x) * 1.0;
c2 = (p3.y * p4.x - p4.y * p3.x) * 1.0;
x = (b1 * c2 - b2 * c1) / (b2 * a1 - b1 * a2);
y = (a1 * c2 - c1 * a2) / (a2 * b1 - a1 * b2);
} bool check(point p1, point p2, point p3, point p4)//p1p2是否穿过竖着的p3p4,查看这条线是否与每一个拐角处上下连接的线段都相交,包括端点
{
double d1 = x_multi(p1, p2, p3);
double d2 = x_multi(p1, p2, p4);
return d1 * d2 <= ;
}
bool check2(point p1, point p2, point p3, point p4)//同理看p3, p4这两个点是否在p1p2两侧,端点不算
{
double d1 = x_multi(p1, p2, p3);
double d2 = x_multi(p1, p2, p4);
return d1 * d2 < ;
}
point does(point p1)//它的对应的下一个端点
{
p1.y--;
return p1;
}
int main()
{
while (~scanf("%d", &n) && n)
{
for (int i = ; i < n; i++)
{
scanf("%lf %lf", &p[i].x, &p[i].y);
}
point p0;
double ans = p[].x;
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
if (i == j)
continue;
if (check(p[i], does(p[j]), p[], does(p[])))//如果光线可以从入口射进来
{
for (int k = ; k < n; k++)
{
if (!check(p[i], does(p[j]), p[k], does(p[k])))//如果走到k点这个拐点与管壁相交了,找出相交的点来
{
if (check2(p[i], does(p[j]), p[k], p[k - ]))//如果与上壁相交
{
get_intersection(p[i], does(p[j]), p[k], p[k - ], p0.x, p0.y);
if (ans < p0.x)
ans = p0.x;
break;
}
if (check2(p[i], does(p[j]), does(p[k]), does(p[k - ])))//如果与下壁相交
{
get_intersection(p[i], does(p[j]), does(p[k]), does(p[k - ]), p0.x, p0.y);
if (ans < p0.x)
ans = p0.x;
break;
}//如果都不相交的话,那么说明是与上一段的端点相交
if (ans < p[k - ].x)
ans = p[k - ].x;
break;
}
if (k == n - )//如果走到最后都没break,也就是相交,那么说明可以通过这个管道,直接让他等于最后的x坐标
{
ans = p[n - ].x;
}
}
}
}
}
if (sgn(ans - p[n - ].x) == )
{
puts("Through all the pipe.");
}
else
printf("%.2f\n", ans);
}
return ;
}

POJ 1039 Pipe 枚举线段相交的更多相关文章

  1. POJ 1039 直线和线段相交

    题意: 题意很好理解,从左边射过来的光线,最远能经过管道到右边多少距离. 分析: 光线一定经过一个上端点和一个下端点,这一点很容易想到.然后枚举上下端点即可 #include <iostream ...

  2. POJ 1039 Pipe【经典线段与直线相交】

    链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  3. 简单几何(直线与线段相交) POJ 1039 Pipe

    题目传送门 题意:一根管道,有光源从入口发射,问光源最远到达的地方. 分析:黑书上的例题,解法是枚举任意的一个上顶点和一个下顶点(优化后),组成直线,如果直线与所有竖直线段有交点,则表示能穿过管道. ...

  4. POJ 1039 Pipe(直线和线段相交判断,求交点)

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8280   Accepted: 2483 Description ...

  5. poj 1066(枚举+线段相交)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6328   Accepted: 2627 Des ...

  6. POJ 1408 Fishnet【枚举+线段相交+叉积求面积】

    题目: http://poj.org/problem?id=1408 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  7. POJ - 1039 Pipe(计算几何)

    http://poj.org/problem?id=1039 题意 有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从左边入 ...

  8. POJ 1066 Treasure Hunt(线段相交判断)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4797   Accepted: 1998 Des ...

  9. POJ 1066--Treasure Hunt(判断线段相交)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7857   Accepted: 3247 Des ...

随机推荐

  1. 在Ubuntu12.0.4下搭建TFTP服务器

    一.安装相关安装包 tftpd(服务端),tftp(客户端) sudo apt-get install tftp-hpa tftpd-hpa 安装xinetd sudo apt-get install ...

  2. SignalR实现B/S系统对windows服务运行状态的监测

    基于SignalR实现B/S系统对windows服务运行状态的监测 通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windo ...

  3. NSSet与NSArray区别

    NSSet与NSArray区别     NSSet到底什么类型,其实它和NSArray功能性质一样,用于存储对象,属于集合: NSSet  , NSMutableSet类声明编程接口对象,无序的集合, ...

  4. 新建maven project遇到的问题

    在m2e安装成功之后,开始创建maven project了,但是出现了如下错误: 结果在很偶然的情况下让我解决了,就是更新下m2.respository,点击下图中的Update Settings -

  5. temp 临时文件

    放假了,放假了:http://blog.csdn.net/skywalker_only/article/details/17076851 nucht2.2.1爆出如下异常: Exception in ...

  6. BZOJ 1020 安全的航线flight

    Description 在设计航线的时候,安全是一个很重要的问题.首先,最重要的是应采取一切措施确保飞行不会发生任何事故,但同时也需要做好最坏的打算,一旦事故发生,就要确保乘客有尽量高的生还几率.当飞 ...

  7. The Child and Toy

    Codeforces Round #250 (Div. 2) C:http://codeforces.com/problemset/problem/437/C 题意:给以一个无向图,每个点都有一点的权 ...

  8. <未测>源码升级安装glibc和rpm升级glibc

    源码升级安装glibc和rpm升级glibc http://jacklin9.spaces.live.com/blog/cns!A891B52E1182AFB2!346.entry http://bl ...

  9. A Simple problem

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2497 题意:给出顶点数,边数及节点s,判断s是 ...

  10. WordPress Citizen Space插件跨站请求伪造漏洞

    漏洞名称: WordPress Citizen Space插件跨站请求伪造漏洞 CNNVD编号: CNNVD-201307-463 发布时间: 2013-07-23 更新时间: 2013-07-23 ...