简单题,题意就是从y坐标最低的点出发,每次只能向左转,并且不能与之前走过的路相交,问最多能经过多少个点。
这其实就是一个对每个点,找求最小极角的问题首先判断当前的角与堆栈顶部的两个角的夹角是否成左旋,然后找与当前栈顶的点构成的直线角度最小的点入栈,然后重复上面的步骤,直到没有满足条件的点为止。代码如下:
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> using namespace std; double eps=1e-8; bool map[60]; struct Point{ int x,y,index; }plant[60]; Point que[60]; int n; int top; int mult( Point p1, Point p2, Point p3) //p2p1,p2p3的叉积 { return (p1.x - p2.x)*(p3.y - p2.y)-( p1.y - p2.y )*( p3.x - p2.x ); } int cmp(Point a,Point b) { return a.y < b.y ; } void addPlant() { int i,tmp; que[1] = plant[0]; top=1; while(1) { tmp=0; double angle=4.2; for( i = 1; i < n; i++) if( !map[i] && mult( que[top-1] , que[top] , plant[i] )) //判断是否与栈顶成左旋 { int x1 = plant[i].x - que[top].x,x2 = que[top].x - que[top-1].x,y1 = plant[i].y - que[top].y,y2 = que[top].y - que[top-1].y; double ang = acos((x1 * x2 + y1 * y2)/sqrt(double(x1 * x1 + y1 * y1)*(x2 * x2 + y2 * y2))); if(ang < angle - eps) //找角度最小的角 { angle = ang; tmp = i; } } if( tmp ) { que[++top] = plant[tmp]; map[tmp] = 1; } else break; } } int main() { int t,i; scanf("%d",&t); while(t--) { memset(map,0,sizeof(map)); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d %d %d", &plant[i].index , &plant[i].x , &plant[i].y ); sort(plant,plant + n,cmp);//按y坐标排序 que[0].y = plant[0].y; que[0].x = 0; addPlant(); printf("%d",top); for(i=1 ; i<=top ;i++) printf(" %d",que[i].index); printf("\n"); } return 0; }