Meaning of the questions:
Give you a square, and then on the square a given number of points, according to Hilbert curve through these points of order sorting
answer:
Well-defined comparison function after sorting algorithm can be called directly.
Hilbert curve inherently for two-dimensional to one-dimensional mapping, we can consider a point for each of the pre-Hilbert curve which is the first several passes, and then sorted.
As can be seen, the origin is assumed to establish a square in the center, then successively Hilbert curve passing through the origin of upper left, lower left, lower right, upper right, and four quadrants Hilbert trace function is symmetrical to each other, for the different quadrants, the quadrant is first calculated total number of points before it passes, then the relative coordinates of the two points do different symmetry transformation, Hilbert curve recursively to continue to a smaller size.
#include<iostream> #include<algorithm> #include<cassert> #define LL long long using namespace std; inline int pos(const int &x,const int &y){ if(x==0 && y==0)return 0; if(x==1 && y==0)return 1; if(x==1 && y==1)return 2; if(x==0 && y==1)return 3; } struct Hbt{ LL x,y; int size; LL rank; LL calc_rank(){ rank=0; LL xx=x,yy=y; LL w=1<<(size-1); while(w){ int _pos=pos((int)xx/w,(int)yy/w); rank+=w*w*_pos; if(_pos==1_Pos == || 2 ) { // remain undisturbed XX% = W; YY % = W; } the else IF (_pos == 0 ) { // lower left upper right interchangeable XX% = W; YY % = W; the swap ( XX, YY); } the else IF (_pos == . 3 ) { // top left bottom right interchange XX = w to . 1 -XX% W; YY = w to . 1 -YY%w; swap(xx,yy); } w>>=1; } rank+=pos((int)xx,(int)yy); return rank; } friend bool operator <(const Hbt &a,const Hbt &b){ return a.rank<b.rank; } Hbt(){} Hbt(LL a,LL b,int c){ x=a;y=b;size=c; } }p[1000006]; intmain () { // For calculation coordinates Save a memory int n-, K; Scanf ( " % D% D " , & n-, & K); for ( int I = . 1 ; I <= n-; I ++ ) { Scanf ( " %% LLD LLD " , & P [I] .x, & P [I] .y); -P [I] .x; - P [I] .y; P [I] .size = K; P [I] .calc_rank (); } Sort (P + . 1 , P + . 1 + n-); for ( int I = . 1 ; I <= n-; I ++ ){ the printf ("%lld %lld\n",p[i].x+1,p[i].y+1); } return 0; }