POJ 2653:Pick-up sticks _判断两线段是否相交

来源:互联网 发布:小区效果图制作软件 编辑:程序博客网 时间:2024/05/16 15:02

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

51 1 4 22 3 3 11 -2.0 8 41 4 8 23 3 6 -2.030 0 1 11 0 2 12 0 3 10

Sample Output

Top sticks: 2, 4, 5.Top sticks: 1, 2, 3.

Hint

Huge input,scanf is recommended.

Source

Waterloo local 2005.09.17

//题意:往地上随机的扔木棍,问有哪些木棍上面没有压着木棍。直接遍历看当前木棍(线段)与后面的木棍(线段)是否相交!

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<cstdlib>#include<queue>#include<stack>#include<map>#include<vector>#include<algorithm>using namespace std;#define maxn 100005 struct point {double x,y;};struct line {point sp,ep;}L[maxn];int n;bool used[maxn];double x_multi(point p1,point p2,point p3)  {return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);}bool Onsegment(point p1,point p2,point p3)  {double min_x=min(p1.x,p2.x);double min_y=min(p1.y,p2.y);double max_x=max(p1.x,p2.x);double max_y=max(p1.y,p2.y);if(p3.x>=min_x&&p3.x<=max_x&&p3.y>=min_y&&p3.y<=max_y)return true;return false;}bool Is_intersected(point p1,point p2,point p3,point p4)  {double d1=x_multi(p1,p2,p3);double d2=x_multi(p1,p2,p4);double d3=x_multi(p3,p4,p1);double d4=x_multi(p3,p4,p2);if(d1*d2<0.0&&d3*d4<0.0)return true;if(d1==0.0&&Onsegment(p1,p2,p3))  return true;if(d2==0.0&&Onsegment(p1,p2,p4))return true;if(d3==0.0&&Onsegment(p3,p4,p1))return true;if(d4==0.0&&Onsegment(p3,p4,p2))return true;return false;}int main(){while(scanf("%d",&n),n){int i,j;for(i=1;i<=n;i++)scanf("%lf%lf%lf%lf",&L[i].sp.x,&L[i].sp.y,&L[i].ep.x,&L[i].ep.y);memset(used,false,sizeof(used));for(i=1;i<=n;i++)for(j=i+1;j<=n;j++)if(Is_intersected(L[i].sp,L[i].ep,L[j].sp,L[j].ep)){used[i]=true;break;}printf("Top sticks: ");for(i=1;i<=n;i++)if(!used[i]){if(i<n)printf("%d, ",i);elseprintf("%d.\n",i);}}return 0;}



原创粉丝点击