2015年ALPC暑期专题练习I (计算几何) F Pick-up sticks

久违的1A,就是写了个判断线段相交的模板,注意一个线段的端点在另外一个线段上或者端点上,属于被覆盖情况。

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<sstream>
#include<cmath>
using namespace std;
/**********************************************************/
struct point{
  double x,y;
  point (double i,double j){x=i;y=j;}//普通构造函数
  point (const point& p){x=p.x;y=p.y;}//拷贝构造函数
  point& operator = (const point& p){//重载=
    x=p.x;y=p.y;
    return *this;
  }
  point operator - (const point& p){//重载-,b-a => b.-(a) 即线段ab
	return point (p.x-x,p.y-y);
  }
  point (){}
};
struct segment{
  point left,right;
  segment (point a,point b){left=a;right=b;}
  segment (){}
};

const int MAX_NUM = 100000+10;
const double MAX_DOUBLE = 100000000.0;
segment allSegment[MAX_NUM];

double CrossMuti (point a,point b){return a.x*b.y-a.y*b.x;}
double min_2 (double x,double y) {return x<y?x:y;}
double max_2 (double x,double y) {return x>y?x:y;}

/* 0-不相交  1-正常相交  2-端点在线段上  3-重合 */
int ab_cross_cd (point a,point b,point c,point d)
{
  if(min_2(a.x,b.x)>max_2(c.x,d.x)||min_2(a.y,b.y)>max_2(c.y,d.y)||
	 max_2(a.x,b.x)<min_2(c.x,d.x)||max_2(a.y,b.y)<min_2(c.y,d.y) ) return 0;
  point abVec=b-a,acVec=c-a,adVec=d-a;
  double x = CrossMuti (abVec,acVec);
  double y = CrossMuti (abVec,adVec);
  if(x==0.0&&y==0.0)//重合
	return 3;
  else if (x==y)
	return 0;//不相交
  else{
	point cdVec=d-c,caVec=a-c,cbVec=b-c;
	x*=y;
	y=CrossMuti (cdVec,caVec)*CrossMuti (cdVec,cbVec);
	if (x<0 && y<0) return 1;
	else if ((x==0&&y<=0)||(y==0&&x<=0)) return 2;
	else return 0;
  }
}
/**********************************************************/
int main()
{
  //freopen ("in.txt","r",stdin);
  int n;
  while (scanf ("%d",&n)&&n)
  {
	for (int i=0;i<n;i++)
	  scanf ("%lf %lf %lf %lf",&allSegment[i].left.x,&allSegment[i].left.y,&allSegment[i].right.x,&allSegment[i].right.y);
	printf ("Top sticks:");
	for (int i=0;i<n;i++){
	  int vis=1;
	  for (int j=i+1;j<n;j++)
		if (ab_cross_cd (allSegment[i].left,allSegment[i].right,allSegment[j].left,allSegment[j].right)>0){vis=0;break;}
	  if (vis){
		printf (" %d",i+1);
		if (i==n-1)  printf (".\n");
		else printf (",");
	  }
	}
  }
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值