Cod sursa(job #2737526)

Utilizator PinkiePie1189Preoteasa Mircea-Costin PinkiePie1189 Data 4 aprilie 2021 20:23:11
Problema Fractal Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <fstream>
#include <algorithm>


int divide (int k, int x, int y, int flip) {
    if (k == 1) {
        if (flip) {
            if (x == 1 && y == 1) {
                x = 2;
                y = 2;
            } else if (x == 2 && y == 2) {
                x = 1;
                y = 1;
            }
        }

        if (x == 1 && y == 1) {
            return 0;
        }

        if (x == 2 && y == 1) {
            return 3;
        }

        if (x == 1 && y == 2) {
            return 1;
        }

        if (x == 2 && y == 2) {
            return 2;
        }
    }


    int bound = (1 << (k - 1));

    if (x <= bound && y <= bound) {
        return divide(k - 1, y, x, flip);
    }

    if (x <= bound && y > bound) {
        return (2 * bound) + divide(k - 1, x, y - bound, flip);
    }

    if (x > bound && y > bound) {
        return (4 * bound) + divide(k - 1, x - bound, y - bound, flip);
    }

    return (6 * bound) + divide(k - 1, x - bound, y, (flip + 1) % 2);
}
int main() {
    std::ifstream fin("fractal.in");
    std::ofstream fout("fractal.out");
    int k, x, y;
    fin >> k >> x >> y;
    fout << divide(k, x, y, 0);
    return 0;
}