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; }
相关文章推荐
- [POJ 2653]Pick-up sticks
- POJ 2653 Pick-up sticks <计算几何>
- POJ 2653 Pick-up sticks
- POJ2653——Pick-up sticks
- Pick-up sticks POJ - 2653
- 简单几何(线段相交) POJ 2653 Pick-up sticks
- POJ 2653 && HDU 1147 Pick-up sticks(计算几何)
- hdu 1147 pick up sticks 几何问题线段相交
- POJ 2653 Pick_up_stick(计算几何)
- hdu 5543 Pick The Sticks(01背包)
- POJ 2653 Pick-up sticks
- POJ 1564 && HDU 1258 Sum It Up(dfs)
- poj 2653 Pick-up sticks
- poj 2653 Pick-up sticks
- HDU_1051 && POJ_1065 Wooden Sticks(贪心)
- POJ 2653 Pick-up sticks 线段相交问题
- POJ 2653 Pick-up sticks(判断线段相交)
- 判断线段相交 + vector. erase迭代指针 的使用 poj 2653 Pick-up sticks
- POJ 2653 Pick-up sticks(计算几何,判断线段相交)
- POJ 2653 - Pick-up sticks(判断两条直线相交)