Cod sursa(job #3213292)

Utilizator marinaoprOprea Marina marinaopr Data 12 martie 2024 20:52:43
Problema Fractal Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <fstream>

using namespace std;

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

void rotate(int &x, int &y, int n) 
{
    int aux = x;
    
    x = (n + 1) - y;
    y = aux;
}

void solve(int K, int x, int y, int &ans)
{
    //fout << K << " " << y << " " << x << " " << ans << "\n" ;

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

    int n = (1 << K);
    int nextn = (1 << (K - 1));
    if (x <= nextn && y <= nextn) {// cadran 1 
        solve(K - 1, y, x, ans);
        return;
    }

    if (x <= nextn && y > nextn) { // cadran 3
        ans += n;
        y -= nextn;
        rotate(x, y, nextn);
        solve(K - 1, x, y, ans);
        return;
    }

    if (x > nextn && y <= nextn) { // cadran 2
        ans += 3 * n;
        x -= nextn;
        rotate(x, y, nextn);
        rotate(x, y, nextn);
        solve(K - 1, x, y, ans);
        return;
    }

    ans += 2 * n;
    x -= nextn;
    y -= nextn;
    rotate(x, y, nextn);
    solve(K - 1, x, y, ans);
    return;
}

int main()
{
    int K, x, y;
    int ans = 0;

    fin >> K >> x >> y;
    solve(K, x, y, ans);
    fout << ans;

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