Source Code
Problem: 1696 | User: Praesidio | |
Memory: 188K | Time: 0MS | |
Language: C++ | Result: Accepted |
- Source Code
//poj1696 #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; #define sqr(x) ((x)*(x)) const double eps = 1e-9; const double pi = acos(-1.0); int sgn (const double &x) { return (x>eps) - (x< -eps); } struct Point { int id; double x,y; Point () { } Point (double x,double y) : x(x),y(y) { } }; typedef Point Vector; Vector operator - (Point A,Point B) {return Vector(A.x-B.x,A.y-B.y); } double operator ^ (Vector A,Vector B) {return A.x*B.x+A.y*B.y; } double operator * (Vector A,Vector B) {return A.x*B.y-A.y*B.x; } bool operator == (Point A,Point B) { return sgn(A.x-B.x)==0 && sgn(A.y-B.y)==0 ; } struct Line { Point p; Point q; Vector v; double ang; double ang2; Line () {} Line (Point p,Point q) : p(p),q(q) { v = q-p; ang = atan2 (v.y , v.x); if (sgn(ang)==-1) ang += 2*pi; } bool operator < (const Line& L) const { return ang<L.ang; } }; int n; Point point[55]; Line line[55]; int vis[55]; void init() { scanf("%d",&n); for (int i=0;i<n;i++) { scanf("%d%lf%lf",&point[i].id,&point[i].x,&point[i].y); } } bool cmp(Point A,Point B) { return (A.y != B.y)? A.y<B.y : A.x<B.x ; } int main() { int test; scanf("%d",&test); while (test--) { init(); sort (point,point+n,cmp); printf("%d ",n); Point pole = point[0]; memset(vis,0,sizeof(vis)); int tmp=n-1; double angle=0; printf("%d",pole.id); while (tmp--) { vis[pole.id]=1; int top=0; for (int i=0;i<n;i++) { if (vis[point[i].id]) continue; line[top] = Line ( pole , point[i] ); line[top].ang2 = line[top].ang; line[top].ang -= angle; if (sgn(line[top].ang)==-1) line[top].ang += 2*pi; top++; } sort(line,line+top); pole = line[0].q; angle= line[0].ang2 ; printf(" %d",pole.id); } printf("\n"); } return 0; }