Cod sursa(job #3287095)

Utilizator Octa476Osnaga Octavian Alexandru Octa476 Data 15 martie 2025 14:00:16
Problema Fractal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;

int fractal(int k, int x, int y) {
    if (!k)
        return 0;
    int mean = 1 << (k - 1);

    int cost = pow(4, (k - 1)) - 1;
    int costs[4] = {0, cost + 1, 2 * cost + 2, 3 * cost + 3};
    
    if ((x <= mean) && (y <= mean)) {
        return costs[0] + fractal(k - 1, y, x);
    }
    if ((x <= mean) && (y > mean)) {
        return costs[1] + fractal(k - 1, x, y - mean);
    }
    if ((x > mean) && (y > mean)) {
        return costs[2] + fractal(k - 1, x - mean, y - mean);
    }
    if ((x > mean) && (y <= mean)) {
        return  costs[3] + fractal(k - 1, mean - y + 1, 2 * mean - x + 1);
    }
    
    return 0;
}

int main() {
    ifstream fin("fractal.in");
    int k, x, y;
    fin >> k >> x >> y;
    fin.close();

    ofstream fout("fractal.out");
    fout << fractal(k, x, y);
    fout.close();

    return 0;

}