您的位置:首页 > 其它

HDU 1147 poj 2653 Pick-up sticks

2011-08-16 20:18 507 查看
该题与HDU1086相似都是线段相交的问题,在这里就不解释线段相交了( 在HDU 1086 You can Solve a Geometry Problem too已经解释了)不过我的代码还是注释一下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct T
{
double x,y;
}point;
point start[200024],end[200024];
inline double judge( point p1,point p2,point p )//判断点是否在直线的两边
{
return ( ( p1.x-p.x )*( p2.y-p.y ) - ( p2.x-p.x )*( p1.y-p.y ) );
}
inline bool on_megment( point p1,point p2,point p )//判断端点是不是在直线上
{
double max=p1.x>p2.x?p1.x:p2.x;//找出直线的左右端点的范围
double min=p1.x<p2.x?p1.x:p2.x;
if( p.x>=min&&p.x<=max )   returntrue;
elsereturnfalse;
}
bool megment( point p1,point p2,point q1,point q2 )
{
double d1=judge( p1,p2,q1 );
double d2=judge( p1,p2,q2 );
double d3=judge( q1,q2,p1 );
double d4=judge( q1,q2,p2 );
if( d1*d2<0&&d3*d4<0 )returntrue;//如果都异侧就一定相交
if( d1==0&&on_megment( p1,p2,q1 ) )  returntrue;//d为0是平行的情况,这是我们就要考虑是不是端点在直线上
if( d2==0&&on_megment( p1,p2,q2 ) )  returntrue;
if( d3==0&&on_megment( q1,q2,p1 ) )  returntrue;
if( d4==0&&on_megment( q1,q2,p2 ) )  returntrue;
returnfalse;
}
int main()
{
int n,hash[100024];

while( scanf( "%d",&n ),n )
{
memset( hash, 0,sizeof( hash ) );
int count=0;
for( int i=1; i<=n; i++ )
scanf( "%lf%lf%lf%lf",&start[i].x,&start[i].y,&end[i].x,&end[i].y );
for( int i=1;i<=n; i++ )
for( int j=i+1;j<=n; j++ )
{
if( megment( start[i],end[i],start[j],end[j] ) )
{
hash[i]=1;//记录被覆盖的棍子
count++;
break;
}
}
int sum=0;
count=n-count;
printf( "Top sticks:" );
for( int i=1;i<=n; i++ )
{
if( 0==hash[i] )
{
sum++;
printf( count==sum?" %d.\n":" %d,",i );
}
}
}
return0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: