Cod sursa(job #2552953)

Utilizator memecoinMeme Coin memecoin Data 21 februarie 2020 13:11:29
Problema Fractal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <stack>

#define INF 0x3f3f3f3f

using namespace std;

#ifdef DEBUG
string name = "data";
#else
string name = "fractal";
#endif

ifstream fin(name + ".in");
ofstream fout(name + ".out");

int rot[4] = {1,4,2,3};

int hilbert(int x, int y, int k) {

    if (k == 1) {
        int ndx = (y - 1) * 2 + (x - 1);
        return rot[ndx];
    }
    
    int sz = 1 << (k - 1);
    int szd = sz * sz;
    
    if (x > sz && y > sz) {
        return 2 * szd + hilbert(x - sz, y - sz, k - 1);
    }
    
    if (y > sz) {
        return szd + hilbert(x, y - sz, k - 1);
    }
    
    if (x > sz) {
        return 3 * szd + hilbert(sz - y + 1, sz - (x - sz) + 1, k - 1);
    }
    
    return hilbert(y, x, k - 1);
}

int main() {
    
    int k, x, y;
    
    fin >> k >> x >> y;

    fout << hilbert(x, y, k) - 1;
    
//    k = 3;
//    int n = 1 << k;
//
//    for (int y = 1; y <= n; ++y) {
//        for (int x = 1; x <= n; ++x) {
//            fout << hilbert(x, y, k) << " ";
//        }
//        fout << "\n";
//    }
    
    return 0;
}