您的位置:首页 > 其它

POJ 2653 Pick-up sticks【计算几何入门】

2016-03-24 20:21 399 查看
http://poj.org/problem?id=2653

Pick-up sticks

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 11638 Accepted: 4390
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.


题意:

在一个平面扔线段,问最后那些线段上没有线段压着,并输出这些线段

思路:

把每条线段与比它后扔下的线段判断是否相交,相交这这条线段上面还被线段压着,这时候就可以判断下一条线段了。



AC代码

#include <stdio.h>
#include <string.h>
#define MAXN 100000
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
int ans[MAXN+100];
typedef struct
{
double x,y;
}point;
point arr1[MAXN+100];
point arr2[MAXN+100];
double direction(point p1,point p3,point p4)//向量p3p1和向量p3p4的叉乘
{
return (p1.x-p3.x)*(p4.y-p3.y)-((p4.x-p3.x))*(p1.y-p3.y);
}
bool judge(point p1,point p2,point p3,point p4)
{
double d1=direction(p1,p3,p4);
double d2=direction(p2,p3,p4);
double d3=direction(p3,p1,p2);
double d4=direction(p4,p1,p2);
if(d1*d2<=0&&d3*d4<=0)
return 1;
else return 0;
}
bool quickTest(point p1,point p2,point p3,point p4)//快速排斥实验
{
if(min(p1.x,p2.x)<=max(p3.x,p4.x)
&&min(p3.x,p4.x)<=max(p1.x,p2.x)
&&min(p1.y,p2.y)<=max(p3.y,p4.y)
&&min(p3.y,p4.y)<=max(p1.y,p2.y))
{
return judge(p1,p2,p3,p4);
}
else return 0;
}
int main()
{
int N;
bool flag[MAXN+100];
while(~scanf("%d",&N),N)
{
memset(flag,0,sizeof(flag));//表示第i条直线上方是否存在直线
for(int i=1;i<=N;i++)
{
scanf("%lf%lf%lf%lf",&arr1[i].x,&arr1[i].y,&arr2[i].x,&arr2[i].y);
}
for(int i=1;i<=N;++i)
{
for(int j=i+1;j<=N;++j)
{
if(judge(arr1[i],arr2[i],arr1[j],arr2[j]))
{
flag[i]=1;
break;//没这个绝对超时
}
}
}
printf("Top sticks: ");
int j=0;
for(int i=1;i<=N;++i)
{
if(!flag[i])
ans[j++]=i;
}
for(int i=0;i<j-1;++i)
printf("%d, ",ans[i]);
printf("%d.\n",ans[j-1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: