poj1039 Pipe【计算几何】

含【求直线交点】、【判断直线与线段相交】模板

Pipe

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:11940   Accepted: 3730

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
0

Sample Output

4.67
Through all the pipe.

Source

Central Europe 1995

题意:

给n个点 构成两条平行的折线

问从管道口出发的光线最远能到达的横坐标

思路:

最远的光线一定是贴着管道的某两个端点走的

现在枚举这两个端点  判断其与后面折线的交点

刚开始没想到判断交点时 可以先判断line 和 line(up[k], down[k])

这样得到的k就是最小的不能达到的k

用这个k就可以拿来算line 和 line(up[k-1], up[k])以及line(down[k -1], down[k])的交点了

  1 //#include <bits/stdc++.h>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<stdio.h>
  6 #include<cstring>
  7
  8 using namespace std;
  9 typedef long long int LL;
 10
 11 #define zero(x) (((x) > 0? (x) : -(x)) < eps)
 12 const double eps = 1e-8;
 13 int sgn(double x)
 14 {
 15     if(fabs(x) < eps) return 0;
 16     if(x < 0) return -1;
 17     else return 1;
 18 }
 19
 20 struct point{
 21     double x, y;
 22     point(){}
 23     point(double _x, double _y)
 24     {
 25         x = _x;
 26         y = _y;
 27     }
 28     point operator -(const point &b)const
 29     {
 30         return point(x - b.x, y - b.y);
 31     }
 32     double operator ^(const point &b)const
 33     {
 34         return x * b.y - y * b.x;
 35     }
 36     double operator *(const point &b)const
 37     {
 38         return x * b.x + y * b.y;
 39     }
 40     void input()
 41     {
 42         scanf("%lf%lf", &x, &y);
 43     }
 44 };
 45
 46 struct line{
 47     point s, e;
 48     line(){}
 49     line(point _s, point _e)
 50     {
 51         s = _s;
 52         e = _e;
 53     }
 54     //0表示直线重合,1表示平行,2相交
 55     pair<int, point>operator &(const line &b)const
 56     {
 57         point res = s;
 58         if(sgn((s - e) ^ (b.s - b.e)) == 0){
 59             if(sgn((s - b.e) ^ (b.s - b.e)) == 0){
 60                 return make_pair(0, res);
 61             }
 62             else return make_pair(1, res);
 63         }
 64         double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e));
 65         res.x += (e.x - s.x) * t;
 66         res.y += (e.y - s.y) * t;
 67         return make_pair(2, res);
 68     }
 69 };
 70
 71 //判断直线与线段相交
 72 bool seg_inter_line(line l1, line l2)
 73 {
 74     return sgn((l2.s - l1.e) ^ (l1.s - l1.e)) * sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= 0;
 75 }
 76
 77 int n;
 78 point up[100], down[100];
 79 int main()
 80 {
 81     while(scanf("%d", &n) != EOF && n != 0){
 82         for(int i = 0; i < n; i++){
 83             up[i].input();
 84             down[i].x = up[i].x;
 85             down[i].y = up[i].y - 1.0;
 86         }
 87
 88
 89         bool flag = false;
 90         double ans = -100000000.0;
 91         int k;
 92         for(int i = 0; i < n; i++){
 93             for(int j = i + 1; j < n; j++){
 94                 for(k = 0; k < n; k++){
 95                     if(seg_inter_line(line(up[i], down[j]), line(up[k], down[k])) == 0){
 96                         break;
 97                     }
 98                 }
 99                 if(k >= n){
100                     flag = true;
101                     break;
102                 }
103                 if(k > max(i, j)){
104                     if(seg_inter_line(line(up[i], down[j]), line(up[k - 1], up[k]))){
105                         pair<int, point>pr = line(up[i], down[j]) & line(up[k - 1], up[k]);
106                         point p = pr.second;
107                         ans = max(ans, p.x);
108                     }
109                     if(seg_inter_line(line(up[i], down[j]), line(down[k - 1], down[k]))){
110                         pair<int, point>pr = line(up[i], down[j]) & line(down[k - 1], down[k]);
111                         point p = pr.second;
112                         ans = max(ans, p.x);
113                     }
114                 }
115
116                 for(k = 0; k < n; k++){
117                     if(seg_inter_line(line(down[i], up[j]), line(up[k], down[k])) == 0){
118                         break;
119                     }
120                 }
121                 if(k >= n){
122                     flag = true;
123                     break;
124                 }
125                 if(k > max(i, j)){
126                     if(seg_inter_line(line(down[i], up[j]), line(up[k - 1], up[k]))){
127                         pair<int, point>pr = line(down[i], up[j]) & line(up[k - 1], up[k]);
128                         point p = pr.second;
129                         ans = max(ans, p.x);
130                     }
131                     if(seg_inter_line(line(down[i], up[j]), line(down[k - 1], down[k]))){
132                         pair<int, point>pr = line(down[i], up[j]) & line(down[k - 1], down[k]);
133                         point p = pr.second;
134                         ans = max(ans, p.x);
135                     }
136                 }
137             }
138             if(flag){
139                 break;
140             }
141         }
142         //cout<<ans<<endl;
143         if(flag){
144             printf("Through all the pipe.\n");
145         }
146         else{
147             printf("%.2f\n", ans);
148         }
149     }
150     return 0;
151 }

原文地址:https://www.cnblogs.com/wyboooo/p/9737596.html

时间: 2024-07-31 09:58:45

poj1039 Pipe【计算几何】的相关文章

poj 1039 Pipe (计算几何)

Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9110   Accepted: 2755 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 th

POJ1039 Pipe

嘟嘟嘟 大致题意:按顺序给出\(n\)个拐点表示一个管道,注意这些点是管道的上端点,下端点是对应的\((x_i, y_i - 1)\).从管道口射进一束光,问能达到最远的位置的横坐标.若穿过管道,输出\(Through\) \(all\) \(the\) $ pipe.$ 还是线段求交问题. 枚举端点作为直线(光束)上的两个点.然后判断这条直线和每一条线段\((x_i, y_i)(x_i, y_i - 1)\)是否有交点.若无,则求出最远能到达的\(x\). 注意坐标可为负,所以刚开始的极小值为

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

hdoj Pipe&amp;&amp;南阳oj管道问题&amp;&amp;poj1039(计算几何问题...枚举)

Pipe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 240    Accepted Submission(s): 99 Problem Description The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic l

poj1039——计算几何 求直线与线段交点,判断两条直线是否相交

poj1039——计算几何  求直线与线段交点,判断两条直线是否相交 Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9439   Accepted: 2854 Description The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the de

poj1039(计算几何)线段相交

题意:给一个管道求光线能穿到的最大x坐标. 解法:通过旋转光线一定可以使得光线接触一个上点和一个下点.枚举接触的上下点,然后逐一判断光线是否穿过每个拐点面.碰到一个拐点面没有穿过的,则是因为与其左边线段相交,求出直线与线段交点更新答案即可.不想交则说明在前一个拐点已经穿出去了. 代码: /****************************************************** * author:xiefubao ********************************

POJ - 1039 Pipe(计算几何)

http://poj.org/problem?id=1039 题意 有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从左边入口处的(x1,y1),(x1,y1-1)之间射入,向四面八方传播,求解光线最远能传播到哪里(取x坐标)或者是否能穿透整个管道. 分析 最远的直线必定经过一个上折点和一个下折点.枚举这两个点即可. #include <iostream> #include <stdio.h> #inclu