POJ 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.

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.
分析: 一开始从后往前枚举,如果相交,就把前面的线段标记掉,结果TLE, 然后改成从前往后枚举,如果相交标记前面的这条边并Break;

结果400多毫秒k掉了。

View Code
#include<stdio.h>
#include<string.h>
#define clr(x) memset(x,0,sizeof(x))
int v[100005];
double min(double a,double b)
{
if(a<b)
return a;
return b;
}
double max(double a,double b)
{
if(a>b)
return a;
return b;
}
struct node
{
double x1,y1,x2,y2;
}q[100005];
double cha(double x1,double y1,double x2,double y2)
{
return x1*y2-y1*x2; //叉积
}
int kua(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
if(min(x1,x2)>max(x3,x4)||min(y1,y2)>max(y3,y4)||max(x1,x2)<min(x3,x4)||max(y1,y2)<min(y3,y4))
return 0; //排斥实验
if((cha(x3-x4,y3-y4,x1-x4,y1-y4)*cha(x3-x4,y3-y4,x2-x4,y2-y4)<=0)&&(cha(x1-x2,y1-y2,x3-x2,y3-y2)*cha(x1-x2,y1-y2,x4-x2,y4-y2)<=0)) //跨立实验
return 1;
else return 0;
}
int p(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
if(kua(x1,y1,x2,y2,x3,y3,x4,y4)&&kua(x3,y3,x4,y4,x1,y1,x2,y2))
return 1;
return 0;
}
int main()
{
int n,i,j,top;
while(scanf("%d",&n),n)
{
top=0;
clr(v);
for(i=0;i<n;i++)
scanf("%lf%lf%lf%lf",&q[i].x1,&q[i].y1,&q[i].x2,&q[i].y2);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(p(q[i].x1,q[i].y1,q[i].x2,q[i].y2,q[j].x1,q[j].y1,q[j].x2,q[j].y2))
{ v[i]=1;break;}
printf("Top sticks: ");
for(i=n-1;i>=0;i--)
if(!v[i])break;
for(j=0;j<i;j++)
if(!v[j])
printf("%d, ",j+1);
printf("%d.\n",i+1);
}
return 0;
}


转载于:https://www.cnblogs.com/dream-wind/archive/2012/03/15/2397160.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值