More than 2019 cattle off summer school camp (tenth field) E-Hilbert Sort (fractal)

> Portal <

The meaning of problems


You are now given coordinates $ n $ and $ k $, $ n let you arrange according to given $ k $-order Hilbert curve $ coordinates

Hilbert curve as follows:

      $ k = 1 $ $ k = 2 $ $ k = 3 $
a side length of $ 2 ^ {k} $ flat square can be divided into four parts: ① ② the top left top right bottom left bottom right ③ ④

$ K $ order Hilbert curve by $ k-1 $ Release Order Hilbert curve;

 (1) $ k-1 $ Hilbert curve according to the order obtained by inverting the main diagonal ① portion;

 (2) $ k-1 $ order Hilbert curve copied to ②③ portion;

 (3) $ k-1 $ in accordance with the sub-order Hilbert curve ④ obtained by inverting the diagonal portion;

As shown above, respectively, the first-order, second order, third-order Hilbert curve

analysis


Well-defined comparison function after sorting algorithm can be called directly.

Hilbert curve inherently for two-dimensional to one-dimensional mapping , so 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.

Establish the actual title of the coordinate system is such that (here I planted a pit, no wonder how it all wrong)

 

Code (this is not so simple, but higher readability)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
struct node{
    ll x, y, f;
    bool operator<(const node& a)const{
        return f<a.f;
    }
}p[maxn];
 
int f(ll k,ll x,ll y){
    if(k==0) return 1;
    ll n = 1<<(k-1);
    if(x<=n){
        if(y<=n)return f(k-1,y,x);
        y-=n; return f(k-1,n-y+1,n-x+1)+n*n*3;
    }else{x-=n;
        if(y<=n)return f(k-1,x,y)+n*n;
        y-=n;return f(k-1,x,y)+n*n*2;
    }

}

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i = 0; i < n; i++){
        scanf("%lld%lld", &p[i].x, &p[i].y);
        p[i].f = f(k,p[i].x,p[i].y);
    }
    sort(p, p+n);
    for(int i = 0; i < n; i++) printf("%lld %lld\n", p[i].x, p[i].y);
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/wizarderror/p/11431775.html