poj2653-Pick-up sticks(线段相交问题)

Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9883 Accepted: 3667

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


#include<stdio.h>
#include<math.h>
#define MAXN 100005
const double epsi=1e-10;
inline int	sign(const double &x);
inline double sqr(const double &x);
	
struct Point{
	double x,y;

	Point(double _x=0,double _y=0):x(_x),y(_y){
	}
	Point operator +(const Point &op2) const{
	return Point(x+op2.x,y+op2.y);
	}
	
Point operator -(const Point &op2) const{
	return Point(x-op2.x,y-op2.y);
	}
	double operator ^(const Point &op2) const{
	return x*op2.y-y*op2.x;
	}
		double operator *(const Point &op2) const{
	return x*op2.x+y*op2.y;
	}
	Point operator *(const double &d) const{
	return Point(x*d,y*d);
	}
	Point operator /(const double &d) const{
	return Point(x/d,y/d);
	}
	

	bool operator ==(const Point &op2) const{
	return sign(x-op2.x)==0&&sign(y-op2.y)==0;
	}
	
};

inline int	sign(const double &x){
		if(x>epsi) return 1;
		if(x<-epsi) return -1;
		return 0;	
		}
		
inline double mul(const Point &p0,const Point &p1,const Point &p2){//p0p1与p0p2叉积 
	return (p1-p0)^(p2-p0);
}
	inline double sqr(const double &x){
		return x*x;
	}
inline double dis2(const Point &p0,const Point &p1){//p0p1平方 
	return sqr(p0.x-p1.x)+sqr(p0.y-p1.y);
}
	inline double dis(const Point &p0,const Point &p1){//p0p1
	return sqrt(dis2(p0,p1));
}
inline int cross(const Point &p1,const Point &p2,const Point &p3,const Point &p4){//求p1p2,p3p4是否跨立
int sign1=sign(mul(p1,p3,p2));//求p1p3和p1p2叉积,
 int sign2=sign(mul(p1,p4,p2));//求p1p4和p1p2叉积,
 if(sign1==sign2) return 0;//不跨立
 if(sign1==0&&sign2==0) return 2;//p3或p4在p1p2上
 return 1; 
 
}
bool visit[MAXN];
Point p1[MAXN],p2[MAXN];
int main(){
	int n;
	while(scanf("%d",&n),n){
		for(int i=0;i<n;i++){
			scanf("%lf%lf%lf%lf",&p1[i].x,&p1[i].y,&p2[i].x,&p2[i].y);
			visit[i]=1;
		}
		printf("Top sticks:");
//		bool flag=1;
		for(int i=0;i<n;i++){
//			flag=1;
			for(int j=i+1;j<n;j++){
				if(cross(p1[i],p2[i],p1[j],p2[j])&&cross(p1[j],p2[j],p1[i],p2[i])) {
					visit[i]=0;break;
				}
				
			}
			if(i==n-1) {
			printf(" %d.\n",i+1);
			break;
			}
			if(visit[i]) printf(" %d,",i+1);
		}
		
	}
	
	
	return 0;
}

题目思路:判断前面丢的是否和后面丢的相交,采用直线跨立:
如p1p2 和p3p4判断是否相交,做辅助线p1p3和p1p4
求p1p3和p1p2的叉积还有p1p4和p1p2的叉积。如果符号相同,表明在p3p4在p1p2同一边 ,则我们说线段p1p2跨立直线P3P4
如果符号不同,则表明在两边,不跨立
如果其中一个为0则P3 或P4 在直线P1P2 上,也算跨立。

所以只要互相判断是否跨立就行了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值