Cod sursa(job #2139940)

Utilizator firutibogdanFiruti Bogdan-Cristian firutibogdan Data 22 februarie 2018 21:42:20
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <fstream>
#include <iostream>

using namespace std;

int fractal_size(int k) {
    if (k == 1) {
        return 3;
    }
    return  4 * fractal_size(k-1) + 3;
}

int fractal(int k, int x, int y) {
    int size = (1 << (k - 1));
    if (k == 1) {
        if (x == 1 && y == 1) {
            return 0;
        } else if (x == 2 && y == 1) {
            return 1;
        } else if (x == 2 && y == 2) {
            return 2;
        } else {
            return 3;
        }
    }
    if ((x <= size) && (y <= size)) {
        return fractal(k - 1, y, x);
    } else if ((x > size) && (y <= size)) {
        return 1 + fractal_size(k - 1) + fractal(k - 1, x - size, y);
    } else if ((x > size) && (y > size)) {
        return 2 + 2 * fractal_size(k - 1) + fractal(k - 1, x - size, y - size);
    } else {
        return 3 + 3 * fractal_size(k - 1) + fractal(k - 1, 2 * size - y + 1, size - x + 1);
    }
}

int main() {

    ifstream fin("fractal.in");
    ofstream fout("fractal.out");

    int k, x, y, size;
    fin >> k >> y >> x;
    size = (1 << k);
    fout << fractal(k, x, y);

    fin.close();
    fout.close();
    return 0;
}