POJ 1039 Pipe 枚举线段相交
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
0Sample Output
4.67
Through all the pipe.Source
/*************************************************************************
> 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 枚举线段相交的更多相关文章
- POJ 1039 直线和线段相交
题意: 题意很好理解,从左边射过来的光线,最远能经过管道到右边多少距离. 分析: 光线一定经过一个上端点和一个下端点,这一点很容易想到.然后枚举上下端点即可 #include <iostream ...
- POJ 1039 Pipe【经典线段与直线相交】
链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- 简单几何(直线与线段相交) POJ 1039 Pipe
题目传送门 题意:一根管道,有光源从入口发射,问光源最远到达的地方. 分析:黑书上的例题,解法是枚举任意的一个上顶点和一个下顶点(优化后),组成直线,如果直线与所有竖直线段有交点,则表示能穿过管道. ...
- POJ 1039 Pipe(直线和线段相交判断,求交点)
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8280 Accepted: 2483 Description ...
- poj 1066(枚举+线段相交)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6328 Accepted: 2627 Des ...
- POJ 1408 Fishnet【枚举+线段相交+叉积求面积】
题目: http://poj.org/problem?id=1408 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- POJ - 1039 Pipe(计算几何)
http://poj.org/problem?id=1039 题意 有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从左边入 ...
- POJ 1066 Treasure Hunt(线段相交判断)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4797 Accepted: 1998 Des ...
- POJ 1066--Treasure Hunt(判断线段相交)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7857 Accepted: 3247 Des ...
随机推荐
- jquery如何判断div是否隐藏--useful
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 这个知识点不错,,学习一下先。。。无状态服务(stateless service)(转)
这样的应用,显得高级一些哟~~:) +================== http://kyfxbl.iteye.com/blog/1831869 ========================= ...
- Windows Server 2012 R2里十个极好的新功能
Windows Server 2012 R2具备的众多新特点大大的增强了操作系统的功能性,同时也是在Windows Server 2012原有功能上的拓展.这里整理出Windows Server 20 ...
- AOP小结
AOP主要采用代理模式来实现的,静态代理(设计模式中的代理模式),动态代理(反射机制,实现InvocationHandler接口),cglib实现(采用继承方式,针对目标类生成子类,并覆盖方法进行增强 ...
- SVN:分支合并到主干
合并日志: --- Merging r173674 through r175986 into '.': C src/test/java/com/test/rigel/sandbox/organizat ...
- BZOJ1679: [Usaco2005 Jan]Moo Volume 牛的呼声
1679: [Usaco2005 Jan]Moo Volume 牛的呼声 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 723 Solved: 346[ ...
- GitHub的5人骨干小组:早期初创公司该如何招到正确的人
转自:http://news.cnblogs.com/n/190924/ 前 5 年对初创公司来说至关重要,根据美国中小企业发展署的数据,大约 1/4 的初创公司在第一年内关门大吉,只有不到一半的企业 ...
- HDU 4283 You Are the One
题意:给定n(n<=100)个人,每个人有个固定的屌丝值D. 起初这些人是站成一行,当第i个人第j个去面试的时候他的值是 Di*j. 要求所有人面试之后 这些值加起来是最小的. 队伍站成一行(其 ...
- 数据结构(Splay平衡树):COGS 339. [NOI2005] 维护数列
339. [NOI2005] 维护数列 时间限制:3 s 内存限制:256 MB [问题描述] 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际 ...
- HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...