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 ...
随机推荐
- yii 验证用户名是否存在 array("name","unique",'message'=>'用户名已经存在'),
//验证用户名是否存在 array("name","unique",'message'=>'用户名已经存在'),
- DEDECMS栏目自定义字段添加
用到的文件: catalog_add.htm 路径:\dede\templets\ catalog_edit.htm 路径:\dede\templets\ catalog_add.php 路径 ...
- python学习第五天 List和tuple类型介绍及其List切片
List 和tuple: python提供一种类似C语言数组的类型,但是使用起来确是相当的简洁.那就讲讲这神奇的python中list 和tuple吧. List类型: 1.直接贴代码: L = [' ...
- ASP.NET MVC轻教程 Step By Step 2 ——View初探
在上一节我们完成了一个最简化的MVC程序,最重要的是下面这段代码. public class HomeController : Controller { public string Index() { ...
- Laravel框架——自己写的类找不到
composer.json my model files are stored in directory of app\models, therefor "autoload": { ...
- 解决 SQL Server 耗尽内存的情况
如果您碰到SQL Server服务造成内存不断扩展最终系统死机等情况,请按照以下方法解决. 原理:SQL Server 2000引入的动态内存分配机制,一般不能很好的回收内存,如果计算机一直不关 ...
- Java---计算机贷款支付额计算(用对话框实现)
本例演示如何编写程序来计算贷款支付问题. 下面是编写程序的步骤: 1.提示用户输入年利率.年数和贷款总额 2.利用年利率算出月利率 3.通过前面的公式计算月支付额. 4.计算总支付额,它是月支付额乘以 ...
- cf601A The Two Routes
A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- WebView js 调用Java本地方法
webView = (WebView) this.findViewById(R.id.webview); WebSettings webSettings = webView.getSettings() ...
- 外星人的供给站 (区间覆盖 t贪心)
/** 区间覆盖问题 分析: 每个点可以确定两个圆心 圆心的范围形成 一个区间 在这个区间上以任意一点画圆便可将此点 包含在内 如果有两个点所确定的区间相交了 说明这两个点可以用一个圆包含在内 即用一 ...
