POJ 1696 Space Ant 极角排序
题意:起点为纵坐标最小的点,每次只能左转,输出点的id
思路:每次按照最新的点进行极角排序
#include <iostream> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <cstdio> using namespace std; #define eps 1e-8 double pi=2*asin(1.0); struct point { int id; double x,y; point() { } point(double xx,double yy) { x=xx; y=yy; } } p[105],C,D; double dist(point a,point b) { return hypot(fabs(a.x-b.x),fabs(a.y-b.y)); } double cross(point a,point b,point c) { return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); } bool cmp(point a,point b) { double tp=cross(C,a,b); if(fabs(tp)<eps) return 0; else if(tp<0) return 0; return 1; } int main() { int T,i,j,k,m,n; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=0; i<n; i++) { scanf("%d%lf%lf",&p[i].id,&p[i].x,&p[i].y); if(p[i].y<p[0].y) swap(p[0],p[i]); else if(p[i].y==p[0].y) swap(p[0],p[i]); } for(i=1; i<n; i++) { C=p[i-1]; sort(p+i,p+n,cmp); } printf("%d",n); for(i=0; i<n; i++) printf(" %d",p[i].id); printf("\n"); } }