POJ2653 Pick-up sticks(线段相交)
Posted hyp1231
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2653 Pick-up sticks(线段相交)相关的知识,希望对你有一定的参考价值。
题目链接:
http://poj.org/problem?id=2653
题目描述:
Pick-up sticks
Description
Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
Input
Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.
Output
For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.
The picture to the right below illustrates the first case from input.
The picture to the right below illustrates the first case from input.
Sample Input
5 1 1 4 2 2 3 3 1 1 -2.0 8 4 1 4 8 2 3 3 6 -2.0 3 0 0 1 1 1 0 2 1 2 0 3 1 0
Sample Output
Top sticks: 2, 4, 5. Top sticks: 1, 2, 3.
Hint
Huge input,scanf is recommended.
题目大意:
按顺序往二维平面上扔线段,一根压在一根上面,求哪些线段在最顶端
思路:
每次新扔一个线段的时候,判断与当前在顶端的线段是否相交,若相交则把之前的那条删除
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 using namespace std; 9 10 const int N = 100010; 11 12 const double EPS = 1e-10; //精度系数 13 14 struct Point { 15 double x, y; 16 Point(double x = 0, double y = 0) :x(x), y(y) {} 17 }; //点的定义 18 19 typedef Point Vector; //向量的定义 20 21 int dcmp(double x) { 22 if (fabs(x) < EPS)return 0; else return x < 0 ? -1 : 1; 23 } //与0的关系 24 25 Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } //向量减法 26 27 double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } //向量叉乘 28 29 bool SegmentProperIntersection(Point& a1, Point& a2, Point& b1, Point& b2) { 30 double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1), 31 c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1); 32 return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0; 33 } //判断两线段相交(恰有一个公共点且不在端点) 34 35 int n; 36 Point A[N], B[N]; 37 38 int main() { 39 while (cin >> n&&n) { 40 vector<int> ve; 41 for (int i = 1; i <= n; ++i) { 42 scanf("%lf%lf%lf%lf", &A[i].x, &A[i].y, &B[i].x, &B[i].y); 43 queue<int> q; 44 for (int j = 0; j < (int)ve.size(); ++j) //枚举当前顶端线段 45 if (SegmentProperIntersection(A[i], B[i], A[ve[j]], B[ve[j]])) 46 q.push(ve[j]); 47 while (!q.empty()) { 48 int t = q.front(); q.pop(); 49 vector<int>::iterator it = lower_bound(ve.begin(), ve.end(), t); 50 ve.erase(it); 51 } 52 ve.push_back(i); 53 } 54 printf("Top sticks: "); 55 for (int i = 0; i < (int)ve.size(); ++i) { 56 if (i)printf(", "); 57 printf("%d", ve[i]); 58 } 59 printf(".\n"); 60 } 61 }
以上是关于POJ2653 Pick-up sticks(线段相交)的主要内容,如果未能解决你的问题,请参考以下文章
POJ 2653 Pick-up sticks [计算几何,线段相交]
POJ 2653--Pick-up sticks(判断线段相交)