首页 > 代码库 > poj1696Space Ant(逆时针螺旋形)

poj1696Space Ant(逆时针螺旋形)

链接

贪心做法,没次找最外面的点,也就是相对前面那条线偏转角度最小的点,除第一个点需要找到最下面的点即Y坐标最小,其余的每次进行极角排序。

 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set>10 using namespace std;11 #define N 5512 #define LL long long13 #define INF 0xfffffff14 const double eps = 1e-8;15 const double pi = acos(-1.0);16 const double inf = ~0u>>2;17 int o[55];18 struct Point19 {20     int x,y;21     Point(double x=0,double y=0):x(x),y(y) {}22     int id;23 }p[N];24 int cc;25 typedef Point pointt;26 pointt operator + (Point a,Point b)27 {28     return Point(a.x+b.x,a.y+b.y);29 }30 pointt operator - (Point a,Point b)31 {32     return Point(a.x-b.x,a.y-b.y);33 }34 double dis(Point a)35 {36     return sqrt(1.0*a.x*a.x+a.y*a.y);37 }38 int cross(Point a,Point b)39 {40     return a.x*b.y-a.y*b.x;41 }42 int mul(Point p0,Point p1,Point p2)43 {44     return cross(p1-p0,p2-p0);45 }46 47 bool cmp(Point a,Point b)48 {49     if(mul(p[cc],a,b)==0)50         return dis(a-p[cc])<dis(b-p[1]);51     else52         return mul(p[cc],a,b)>0;53 }54 int main()55 {56     int t,i,n;57     cin>>t;58     while(t--)59     {60         scanf("%d",&n);61         for(i = 1; i <= n ;i++)62         {63             scanf("%d%d%d",&p[i].id,&p[i].x,&p[i].y);64         }65         int tmp = 1;66         for(i = 2; i <= n; i++)67         if(p[tmp].y>p[i].y)68         {69             tmp = i;70         }71         swap(p[1],p[tmp]);72         int g = 0;73         o[++g] = p[1].id;74         cc = 1;75         for(i = 2 ;i <= n; i++)76         {77             sort(p+i,p+n+1,cmp);78             o[++g] = p[i].id;79             cc = i;80         }81         printf("%d",g);82         for(i = 1 ;i <= g;  i++)83         printf(" %d",o[i]);84         puts("");85     }86     return 0;87 }
View Code