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

POJ 2653 Pick-up sticks 【传送门】

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15915 Accepted: 5999

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.

题目大意:给n个木板,输出:在最顶端没有被其他木板压住的木板的编号。

解题思路:模板题,快速排斥实验,在线段结构体中加个bool变量,代表是否被其他木板压住。输入完之后直接两层循环遍历木板,如果相交在令bool变为false。最后输出所有为true的木板。

在看AC代码之前……先来看看不用排斥实验的后果:

AC代码:

#include<iostream>
#include<complex>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
#define eps 1e-9
#define inf 99999999
typedef long long ll;
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
}; 
typedef Point Vector;

Vector operator + (Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A,double p){return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A,double p){return Vector(A.x/p,A.y/p);}

bool operator < (const Point& a,const Point& b)
{
	return a.x < b.x || (a.x==b.x && a.y<b.y);
}

int dcmp(double x)
{
	if(fabs(x)<eps) 
		return 0;
	else
		return x<0?-1:1;
}

bool operator == (const Point& a,const Point& b)
{
	return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;
}

struct Line{
	Point v,p;
	bool flag;
	//v up\ p down
	//Line(Point v,Point p):v(v),p(p){}
	//Point point(double t){
	//	return v+(p-v)*t;
	//}
};

//点积
double Dot(Vector A,Vector B){ return A.x*B.x+A.y*B.y;}
double Length(Vector A){return sqrt(Dot(A,A));}
double Angle(Vector A,Vector B){return acos(Dot(A,B)/Length(A)/Length(B)); }
 
//叉积
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);}

//向量 旋转,rad是弧度
Vector Rotate(Vector A,double rad)
{
	return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
//计算单位法线
Vector Normal(Vector A)
{
	double L=Length(A);
	return Vector(-A.y/L,A.x/L);
} 

//判断折线bc是不是向ab的逆时针方向转
bool ToLeftTest(Point a,Point b,Point c)
{
	return Cross(b-a,c-b)>0;
 } 
 
//直线交点
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
	Vector u=P-Q;
	//确保Cross(v,w)!=0
	double t=Cross(w,u)/Cross(v,w);
	return P+v*t; 
}

//点到直线距离
double DistanceToLine(Point P,Point A,Point B)
{
	Vector v1=B-A,v2=P-A;
	return fabs(Cross(v1,v2))/Length(v1);
	//如果不取绝对值,得到的是有向距离	
} 

//点到线段的距离
double DistanceToSegment(Point P,Point A,Point B)
{
	if(A==B) return Length(P-A);
	Vector v1=B-A,v2=P-A,v3=P-B;
	if(dcmp(Dot(v1,v2))<0)	return Length(v2);
	else if(dcmp(Dot(v1,v3))>0)	return Length(v3);
	else return fabs(Cross(v1,v2))/Length(v1);	
}  

//点在直线上的投影
Point GetLineProjection(Point P,Point A,Point B)
{
	Vector v=B-A;
	return A+v*(Dot(v,P-A)/Dot(v,v));
} 

//点是否在线段上
bool OnSegment(Point p,Point a1,Point a2)
{
	return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
 } 

//线段相交判定
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){
	double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
		   c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
	return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;	
} 
//快速排斥实验, 
bool Quick_Judge(Point a,Point b,Point c,Point d)
{

    if(!(min(a.x,b.x)<=max(c.x,d.x) && min(c.y,d.y)<=max(a.y,b.y)&&min(c.x,d.x)<=max(a.x,b.x) && min(a.y,b.y)<=max(c.y,d.y)))
        return false;
    double u,v,w,z;
    u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
    v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);
    w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
    z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);
    return (u*v<=0.00000001 && w*z<=0.00000001);
}

//允许在线段端点相交
bool SegmentProperIntersection_duan(Point a1,Point a2,Point b1,Point b2)
{
	double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
		   c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
	//if判断控制是否允许线段在端点处相交,根据需要添加
	if(!dcmp(c1)||!dcmp(c2)||!dcmp(c3)||!dcmp(c4))
	{
		bool f1=OnSegment(b1,a1,a2);
		bool f2=OnSegment(b2,a1,a2);
		bool f3=OnSegment(a1,b1,b2);
		bool f4=OnSegment(a2,b1,b2);
		bool f=(f1|f2|f3|f4);
		return f;	
	}	   
	return (dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0);
} 

//多边形有向面积
//p为端点集合,n为端点个数 
double PolygonArea(Point* p,int n)
{
	double s=0;
	for(int i=0;i<n-1;i++)
		s+=Cross(p[i]-p[0],p[i+1]-p[0]);
	return s/2;
} 

//判断点在多边形内
int isPointPolygon(Point p,vector<Point> poly)
{
	int wn=0;
	int n=poly.size();
	for(int i=0;i<n;i++)
	{
		if(OnSegment(p,poly[i],poly[(i+1)%n]))
			return -1;
		int k=dcmp(Cross(poly[(i+1)%n]-poly[i],p-poly[i]));
		int d1=dcmp(poly[i].y-p.y);
		int d2=dcmp(poly[(i+1)%n].y-p.y);
		if(k>0&&d1<=0&&d2>0) wn++;
		if(k<0&&d2<=0&&d1>0) wn--;
	}	
	if(wn!=0)
		return 1;
	return 0;
} 

//线段排序 
bool cmp(Line a,Line b)
{
	if(a.p==b.p)
		return a.v<b.v;
	return a.p<b.p;
}
//-------------------------------------------------------------------------------------------------------

#define MAXN 100000+5 
Line L[MAXN];
int main()
{
	
	int n;
	while(~scanf("%d",&n) && n)
	{
		int top=1;
		Point a,b;
		double x1,y1,x2,y2;
		for(int i=1;i<=n;i++)
		{
			scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
			a.x=x1,a.y=y1;
			b.x=x2,b.y=y2;
			L[top].p=a,L[top].v=b,L[top].flag=true;
			top++; 
		}
		
		for(int i=1;i<top;i++)
			for(int j=i+1;j<top;j++)
				if(Quick_Judge(L[i].p,L[i].v,L[j].p,L[j].v))
				{
					L[i].flag=false;
					break; 
				}	
		
		int i=1;
		while(!L[i].flag)
			i++;
		printf("Top sticks: %d",i);
		i++;	
		for(;i<=n;i++)
		{
			if(L[i].flag)
				printf(", %d",i);	
		}
		printf(".\n");
		
	}
	
	return 0;
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值