POJ 1039:Pipe 计算几何
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9773 | Accepted: 2984 |
Description
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
with n = 0.
Output
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.
有一个分成几节的水管,水管用上端的断点表示出来,下端与上端断点的距离固定是1。该水管不反射光线,问所有射入该水管的光线中 能到达的最远距离是多少。如果能穿越整个水管,输出Through all the pipe。
套用黑书里面的话。
如果一根光线自始至终未擦到任何顶点,肯定不是最优的(可以通过平移使之优化)。然后,如果只碰到一个顶点,那也不是最优的,可以通过旋转,使它碰到另一个顶点,并且更优。所以最优光线必然是擦到一个上顶点和一个下顶点。其中每一步都是根据目前阻挡光线的管壁的方向,选择使光线更优的操作。
于是有了一个简单的算法,任取一个上顶点和下顶点,形成直线l。若l能射穿左入口,即当x=x0时,直线l在(x0,y0)和(x0,y0 - 1)之间,则是一条可行光线。再从左到右依次判断每条上、下管壁是否与l相交,相交则求交点,并把焦点x值与当前最佳值进行比较,若所有管壁都不与l相交,说明l射穿了整个管道。
代码:
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <string>
- #include <cstring>
- #include <iomanip>
- #pragma warning(disable:4996)
- using namespace std;
- struct point
- {
- double x, y;
- }up[22],down[22];
- const double eps = 1e-3;
- //叉积
- double Multi(point p1, point p2, point p0)
- {
- return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
- }
- //直线ab与线段cd是否相交
- bool Across(point a, point b, point c, point d)
- {
- double tmp = Multi(c, a, b)*Multi(d, a, b);
- if (tmp < 0 || fabs(tmp) < eps)
- return true;
- else
- return false;
- }
- //求两条直线的交点坐标
- double getIntersect(point a, point b, point c, point d)
- {
- double A1 = b.y - a.y;
- double B1 = a.x - b.x;
- double C1 = (b.x - a.x)*a.y - (b.y - a.y)*a.x;
- double A2 = d.y - c.y;
- double B2 = c.x - d.x;
- double C2 = (d.x - c.x)*c.y - (d.y - c.y)*c.x;
- double x = (C2*B1 - C1*B2) / (A1*B2 - A2*B1);
- double y = (C1*A2 - C2*A1) / (A1*B2 - A2*B1);
- return x;
- }
- int main()
- {
- int n, i, j, k;
- double res;
- bool flag;
- while (scanf("%d", &n) && n)
- {
- for (i = 0; i < n; i++)
- {
- scanf("%lf%lf", &up[i].x, &up[i].y);
- down[i].x = up[i].x;
- down[i].y = up[i].y - 1;
- }
- res = up[0].x;
- flag = false;
- for (i = 0; i < n&&!flag; i++)
- {
- for (j = 0; j < n&&!flag; j++)
- {
- if (i == j)continue;
- for (k = 0; k < n; k++)
- {
- if (!Across(up[i], down[j], up[k], down[k]))
- break;
- }
- if (k == n)
- {
- flag = true;
- }
- else if (k > max(i, j))
- {
- res = max(res, getIntersect(up[i], down[j], up[k - 1], up[k]));
- res = max(res, getIntersect(up[i], down[j], down[k - 1], down[k]));
- }
- }
- }
- if (flag)
- cout << "Through all the pipe." << endl;
- else
- cout << fixed << setprecision(2) << res << endl;
- }
- return 0;
- }
版权声明:本文为博主原创文章,未经博主允许不得转载。
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: 9932 Accepted: 3045 Description ...
- POJ 1039 Pipe 枚举线段相交
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9493 Accepted: 2877 Description ...
- POJ 1039 Pipe | 线段相交
题目: 给一个管子,有很多转弯处,问从管口的射线射进去最长能射到多远 题解: 根据黑书,可以证明的是这条光线一定经过了一个上顶点和下顶点 所以我们枚举每对上下顶点就可以了 #include<cs ...
随机推荐
- mui搜索框在ios平台上点击多次才弹出键盘的解决方法
今天使用Hbuilder调试手机端时,发现搜索框在安卓系统下,点击一次就可以弹出键盘 但是在iso下非常的不规律,要点击多次 代码实现如下: <div class="mui-input ...
- 牛客NOIPtg day5 B-demo的gcd
一句话题意:给定长度为n的序列,求任意两两之间gcd的积mod 998244353的值. 好像是莫比乌斯反演板子题???(反正noip估计不考这种毒瘤 考场上想到一个类似正解的思路 好像摊下来最多处理 ...
- 关于MySQL连接Navicat Premium 12失败的解决方法
出现问题的原因:MySQL8.0之后更换了加密方式,而这种加密方式客户端不支持 解决:更改加密方式 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysq ...
- 「SP1716」GSS3 - Can you answer these queries III
传送门 Luogu 解题思路 区间最大子段和板子题. 考虑用线段树来做. 对于一个线段树节点所包含区间,它的最大子段和有两种情况,包含中点与不包含. 不包含的情况直接从左右子树转移. 对于包含的情况: ...
- JAVA 集合 List 分组的两种方法
CSDN日报20170219--<程序员的沟通之痛> [技术直播]揭开人工智能神秘的面纱 程序员1月书讯 云端应用征文大赛,秀绝招,赢无人机! JAVA 集合 List 分组的两种方法 2 ...
- Socket传输大文件(发送与接收)
下载 Client using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:接口的基本实现
interface A{ // 定义接口A public static final String AUTHOR = "李兴华" ; // 全局常量 public abstract ...
- HiBench成长笔记——(4) HiBench测试Spark SQL
很多内容之前的博客已经提过,这里不再赘述,详细内容参照本系列前面的博客:https://www.cnblogs.com/ratels/p/10970905.html 和 https://www.cnb ...
- centos下离线安装zip和unzip
首先如果你的centos可以联网,那可以不用看了,直接yum install -y zip unzip就行,非常的痛快! 如果不能联网,像我一样,只能用vpn连上去,做了点限制.那就非常烦了,yum了 ...
- 安装ruby的一些坑
之前一直下载不下来.是因为需要翻墙.