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 ...
随机推荐
- discuz二次开发技巧
discuz二次开发技巧 二次开发大多时候知识设置和处理,如果能够获知模板文件获得的变量数组将大大提高我们的开发效率 获取页面已经定义的变量 <--{eval printf_r(get_defi ...
- CSS3的appearance属性--改变元素的外观
CSS3 appearance 属性 CSS 参考手册 实例 使 div 元素看上去像一个按钮: div { appearance:button; -moz-appearance:button; /* ...
- js动态加载脚本
最近公司的前端地图产品需要做一下模块划分,希望用户用到哪一块的功能再加载哪一块的模块,这样可以提高用户体验. 所以到处查资料研究js动态脚本的加载,不过真让人伤心啊!,网上几乎都是同一篇文章,4种方法 ...
- iptables的设置
一.filter表防火墙(过滤器) iptables -A ( INPUT OUTPUT ) -s 192.1680.1.200 -p ( TCP UDP ICMP ) -i ( eth0 eth1 ...
- ExtJS 4 类系统
ExtJS 4的类系统(class system)进行了一次重大重构,ExtJS4的新架构就是基于这套新的类系统构建的,因此有必要先了解以下这个class system这篇文章分为四章 I: &quo ...
- BZOJ 1823 满汉全席
Description 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而 ...
- javascript content
1. Chrome, Mozila, IE 2. jQuery, underscore, zepto 3. underscore 4. Backbone, AngularJS 5. RequireJS ...
- linux vi查找命令
linux vi查找命令 div id="article_content" class="article_content"> 使用vi编辑器编辑长文 ...
- IOS中UIButton和UIImageView的区别
1.使用场合 UIImageView:如果仅仅是为了显示图片,不需要监听图片的点击事件 UIButton:既要显示图片,又要监听图片等点击事件 2.相同点 都能显示图片 3.不同点 UIButton能 ...
- 【C++基础之十五】内联函数
1.优点 为什么使用内联函数,而不使用宏定义,虽然宏本身采用的展开来替代函数调用的压栈出栈返回等操作,提高了代码的效率,但是会有两个问题: (1)边际效应 宏只是展开代码而已,所以在一些操作符的优先级 ...
