首页 > 代码库 > POJ 2653 线段交
POJ 2653 线段交
思路:
运用队列存储没有被覆盖的木棍,没加入一个棍子,就要判断一下是否队列中的棍子被覆盖,如果被覆盖,就从队列中删除;
线段交判断方法:跨立实验
Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9698 Accepted: 3591
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.
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.
Source
Waterloo local 2005.09.17
<span style="color:#3366ff;">/***************************************************** author : Grant Yuan time : 2014/8/20 13:56 algorithm : 先断交 source : POJ 2653*****************************************************/#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>#include<iterator>#include<cmath>#include<queue>#define MAX1 100007#define MAX2 1007#define eps 1e-8using namespace std;queue<int>ans;struct Point{ double x,y; Point(){} Point(double _x,double _y) { x=_x; y=_y; } Point operator +(const Point&b) { return Point(x+b.x,y+b.y); } Point operator -(const Point&b) { return Point(x-b.x,y-b.y); } double operator *(const Point&b) { return(x*b.x+y*b.y); } double operator ^(const Point&b) { return(x*b.y-y*b.x); } Point operator *(double b) { return Point(x*b,y*b); } double dot(const Point&b) { return x*b.x+y*b.y; } double det(const Point&b) { return x*b.y-y*b.x; }};struct Line{ Point p1,p2; Line(){} Line(Point _p1,Point _p2) { p1=_p1; p2=_p2; }};int n;Line l[MAX1];bool cover(int i,int j){ int flag1=0;int flag2=0; if(((l[i].p1-l[i].p2)^(l[i].p1-l[j].p1))>eps) flag1=1; else if(((l[i].p1-l[i].p2)^(l[i].p1-l[j].p1))<-eps) flag1=-1; if(((l[i].p1-l[i].p2)^(l[i].p1-l[j].p2))>eps) flag2=1; else if(((l[i].p1-l[i].p2)^(l[i].p1-l[j].p2))<-eps) flag2=-1; if(flag1*flag2==-1||flag1==0||flag2==0) return true; else return false;}bool onseg(Point p1,Point p2,Point q){ return fabs((p1-q).det(p2-q))<=eps&&(p1-q).dot(p2-q)<=eps;}Point intersection(Point p1,Point p2,Point q1,Point q2){ return p1+(p2-p1)*(((q2-q1).det(q1-p1))/(q2-q1).det(p2-p1));}int main(){ while(1){ scanf("%d",&n); if(n==0) break; Point p3,p4; while(!ans.empty()) ans.pop(); for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&p3.x,&p3.y,&p4.x,&p4.y); l[i]=Line(p3,p4); } ans.push(1); for(int i=2;i<=n;i++) { ans.push(i); while(!ans.empty()) { int k; k=ans.front();ans.pop(); if(k==i){ans.push(i);break;} if(!(cover(i,k)&&cover(k,i))) { ans.push(k); } } } printf("Top sticks:"); bool first=true; while(!ans.empty()) { if(first){printf(" %d",ans.front());first=false;} else printf(", %d",ans.front()); ans.pop(); } printf(".\n"); } return 0;}</span>