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 ...
随机推荐
- [前端笔记]第三篇:JavaScript
JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一.代码存放位置 J ...
- python自动开发之第二十一天
一.请求周期 url> 路由 > 函数或类 > 返回字符串或者模板语言? 1.Form表单提交: 提交 -> url > 函数或类中的方法 - .... HttpResp ...
- CodeForces 554B(扫房间)
CodeForces 554B Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- find_cmd函数分析
一.概述 1.函数位置 common/command.c 2.函数功能分析 解析命令的关键环节是如何根据输入命令查找对应命令的信息,从而跳转到对应命令的函数处执行程序.这必然涉及到如何存放命令的详细信 ...
- 转:Ubuntu12.04 LTS 使用心得-开机挂载其他分区
1.在/media目录下创建好用来关联你要挂载的分区的文件夹(相当于一个虚拟目录/挂载点,链接/映射到你要挂载的盘符去) 我要挂载4个分区,所以创建了四个挂载点,名字随便取,只要你自己认的出来哪个对应 ...
- Application路径
根目录:StreamingAssets文件夹 #if UNITY_EDITOR string filepath = Application.dataPath +"/StreamingAsse ...
- MySQL ubuntu启动
service mysql start 启动 service mysql restart 重启 service mysql stop 停止 mysql -uroot -ppassword 登入mysq ...
- Dll方式的线程,需要引用这个
{== D6DLLSynchronizer =================================================} {: This unit handles the D6 ...
- PHP去除空白字符
例子1: <?php $var = " This is a beautiful day!"; // 删除字符串中的所有空白字符(不包括全角空格) $var1 = preg_r ...
- Hierarchy Viewer显示视图性能指标
Hierarchy Viewer默认打开“Tree View”窗口无法显示显示Performance indicators: 但选中根视图再点击按钮“Obtain layout times for t ...