您的位置:首页 > 其它

HDU 1086 You can Solve a Geometry Problem too HDU 1147 Pick-up sticks

2012-08-15 21:16 260 查看
这两道题都是规范相交的模板题。

HDU 1086

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1086

View Code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
double x,y;
}start[200010],end[200010];
double judge(node p1,node p2,node p)//判断点是否在直线的两边
{
return ((p1.x-p.x)*(p2.y-p.y)-(p2.x-p.x)*(p1.y-p.y));
}
/*inline bool on_megment(node p1,node p2,node 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 )   return true;
else
return false;
}*/
bool megment( node p1,node p2,node q1,node 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 )return true;//如果都异侧就一定相交
/*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;*/
return false;
}
int main()
{
int n,hash[100010];

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 );
}
}
}
return 0;
}


计算几何的参考资料/content/4005332.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: