poj 1039 Pipe(几何基础)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9932 | Accepted: 3045 |
Description

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
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
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
【思路】
线段直线相交。
如果一条直线没有经过两个拐点一定不是最优的直线,可以通过旋转移动使之更优。
枚举上线顶点,判断相交,求出交点。
【代码】
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = ;
const double eps = 1e-; struct Pt{
double x, y;
}a[N], b[N];
struct Line{ double a, b, c; };
int n;
double ans; double mult(Pt sp, Pt ep, Pt op){
return (sp.x-op.x)*(ep.y-op.y) - (ep.x-op.x)*(sp.y-op.y);
}
Line getLine(Pt p1, Pt p2){
Line ans;
ans.a = p1.y - p2.y;
ans.b = p2.x - p1.x;
ans.c = p1.x*p2.y - p2.x*p1.y;
return ans;
} bool solve(Pt p1, Pt p2, int e){
int i, flag;
for(i = ; i < n-; i ++) {
if(mult(p2, a[i], p1) < -eps || mult(p2, a[i+], p1) < -eps){
flag = ; break;
}
if(mult(p2, b[i], p1) > eps || mult(p2, b[i+], p1) > eps){
flag = ; break;
}
}
if(i == n-) return true; // 没有与任何的管道相交,Through all the pipe.
if(i < e) return false; // 光线不合法。
Line l1, l2; // 光线合法,求出射到的最远距离。
l1 = getLine(p1, p2);
if(flag == ) l2 = getLine(a[i], a[i+]);
else l2 = getLine(b[i], b[i+]);
ans = max(ans, (l1.b*l2.c-l2.b*l1.c)/(l1.a*l2.b-l2.a*l1.b));
return false;
} int main(){
int i, j;
while(scanf("%d", &n) && n){
for(i = ; i < n; i ++){
scanf("%lf%lf", &a[i].x, &a[i].y);
b[i].x = a[i].x;
b[i].y = a[i].y - ;
}
ans = -1e9;
bool flag = ;
if(n < ) flag = ;
for(i = ; i < n; i ++) {
for(j = i + ; j < n; j ++){
flag = solve(a[i], b[j], j);
if(flag) break;
flag = solve(b[i], a[j], j);
if(flag) break;
}
if(flag) break;
}
if(flag) puts("Through all the pipe.");
else printf("%.2lf\n", ans);
}
return ;
}
poj 1039 Pipe(几何基础)的更多相关文章
- poj 1039 Pipe (Geometry)
1039 -- Pipe 理解错题意一个晚上._(:з」∠)_ 题意很容易看懂,就是要求你求出从外面射进一根管子的射线,最远可以射到哪里. 正解的做法是,选择上点和下点各一个,然后对于每个折点位置竖直 ...
- poj 1039 Pipe(叉乘。。。)
题目:http://poj.org/problem?id=1039 题意:有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从 ...
- POJ - 1039 Pipe(计算几何)
http://poj.org/problem?id=1039 题意 有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从左边入 ...
- 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 1039 Pipe
题意:一根管子,中间有一些拐点,给出拐点的上坐标,下坐标为上坐标的纵坐标减1,管子不能透过光线也不能折射光线,问光线能射到最远的点的横坐标. 解法:光线射到最远处的时候一定最少经过两个拐点,枚举每两个 ...
- POJ 1039 Pipe 枚举线段相交
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9493 Accepted: 2877 Description ...
- POJ 1039 Pipe | 线段相交
题目: 给一个管子,有很多转弯处,问从管口的射线射进去最长能射到多远 题解: 根据黑书,可以证明的是这条光线一定经过了一个上顶点和下顶点 所以我们枚举每对上下顶点就可以了 #include<cs ...
随机推荐
- 新建DragonBones动画文件
本篇文章由:http://www.sollyu.com/new-dragonbones-animation-file/ 说明 我在网上找了很久都没找到关于怎么创建一个DragonBones动画的文章, ...
- 【BZOJ1042】【DP + 容斥】[HAOI2008]硬币购物
Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. Input 第一 ...
- 24种设计模式--策略模式【Strategy Pattern】
刘备要到江东娶老婆了,走之前诸葛亮给赵云(伴郎)三个锦囊妙计,说是按天机拆开解决棘手问题,嘿,还别说,真是解决了大问题,搞到最后是周瑜赔了夫人有折兵呀,那咱们先看看这个场景是什么样子的. 先说这个场景 ...
- Swift语言 1小时速学教程
本文由 张渊杰 (网名寂静)编写 Swift语言 1小时速学教程 写在前面的话 有些人可能想, 呵呵, 1小时学一门语言, 你不是搞笑吧, 我想说, 是的, 完全可以, 就要看你怎么学了 要想在1小时 ...
- MySQL事务隔离级别初探
MySQL有四种隔离级别,分别是: READ UNCOMMITTED(未提交读) READ COMMITTED(提交读) REPEATABLE READ (可重复读) SERIALIZABLE(可串行 ...
- document.getElementById获取不到标签值
var apliay=document.getElementById('apliay_ok'); 代码里指定有id="apliay_ok"的标签,但是获取不到,折腾半天原来是因为在 ...
- php设计模式笔记:单例模式
php设计模式笔记:单例模式 意图: 保证一个类仅有一个实例,并且提供一个全局访问点 单例模式有三个特点: 1.一个类只有一个实例2.它必须自行创建这个实例3.必须自行向整个系统提供这个实例 主要实现 ...
- 一个小程序[Socrates]中学到的Perl点滴
1. 抓取网页源文件,只要三行代码 use LWP::Simple; $url='http://music.baidu.com/top/dayhot'; $page=get($url) or die ...
- 4种方法生成二维码 (js 控制canvas 画出 二维码)
随着网络的迅速发展 发展 发展,二维码的应用将会越来越多.同时很多只是很平凡的二维码,请拿起你的手 把这个二维码 设计起来吧.下面分享了几个非常好的二维码设计. 二维码原理: 二维条码/二维码可以分 ...
- python 中对list做减法操作
问题描述:假设我有这样两个list, 一个是list1,list1 = [1, 2, 3, 4, 5] 一个是list2,list2 = [1, 4, 5] ...