Pipe
Time Limit: 1000MS |
Memory Limit: 10000K |
|
Total Submissions: 6888 |
Accepted: 2020 |
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
解题报告:这道题就是让求是否存在一条直线能穿过这样的管道(不能和管道有交点)若存在这输出Through all the pipe.否则就输出存在一条直线和管道第一次相交时的最大的横坐标。
能够到达最远的光线是由管道的转折点决定的。因此这道题就变成了枚举任意两个转折点,求这两个转折点决定的光线能都到达的最远距离X 值,然后不断更新即可!思路和代码参考的网上的,
代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #define Max(a , b) (a > b ? a : b) using namespace std; const int MAX = 25; const double eps = 1e-8; int n; double ans; struct Point { double x; double y; }pa[MAX], pb[MAX]; double Multi(Point p1, Point p2, Point p3) { return (p1.x - p3.x) * (p2.y - p3.y) - (p1.y - p3.y) * (p2.x - p3.x); } double GetX(Point p1, Point p2, Point p3, Point p4)//已知四个点求直线相交的点的坐标 //(这种求法包括了直线斜率不存在或为0的情况下) { double A1 = p2.y - p1.y; double B1 = p1.x - p2.x; double C1 = p1.y * (-B1) - p1.x * A1; double A2 = p4.y - p3.y; double B2 = p3.x - p4.x; double C2 = p3.y *(-B2) - p3.x * A2; return (C2 * B1 - C1 * B2) / (A1 * B2 - A2 * B1);//这儿是求的交点x的坐标 //(C2 * A1 - C1 * A2) / (B1 * A2 - B2 * A1);这是交点y的坐标 } bool Judge(Point p1, Point p2, int j) { int i, flag1; flag1 = 0; for (i = 0; i < n - 1; ++i) { // 如果光线穿过光缆,则所有上侧的点与p1组成的向量都应该在光线的逆时针方向 if (Multi(p2, pa[i], p1) < -eps || Multi(p2, pa[i + 1], p1) < -eps) { flag1 = 1; break; } // 如果光线穿过光缆,则所有下侧的点与p1组成的向量都应该在光线的顺时针方向 if (Multi(p2, pb[i], p1) > eps || Multi(p2, pb[i + 1], p1) > eps) { flag1 = 2; break; } } if (i == n - 1) { return true; } if (i < j) { return false; } Point p3, p4; if (flag1 == 1) { //L2 = GetLine(pa[i], pa[i + 1]);//和上面的线段有交点的情况 p3 = pa[i]; p4 = pa[i + 1]; } else if (flag1 == 2) { //L2 = GetLine(pb[i], pb[i + 1]);//和下面的线段有交点的情况 p3 = pb[i]; p4 = pb[i + 1]; } ans = Max(ans, GetX(p1, p2, p3, p4));//求出交点横坐标的最大值 return false; } int main() { int i, j; bool flag; while (scanf("%d", &n) != EOF && n) { for (i = 0; i < n; ++i) { scanf("%lf%lf", &pa[i].x, &pa[i].y); pb[i].x = pa[i].x; pb[i].y = pa[i].y - 1.0; } ans = -99999999.0; flag = false; if (n < 3) { flag = true; } for (i = 0; i < n && !flag; ++i)//遍历 { for (j = i + 1; j < n; ++j) { if (Judge(pa[i], pb[j], j))//判断是否直线能穿过 { flag = true; break; } if (Judge(pb[i], pa[j], j)) { flag = true; break; } } } if (flag) { printf("Through all the pipe.\n"); } else { printf("%.2lf\n", ans); } } return 0; }