Cod sursa(job #1297162)

Utilizator lavi07Cioloca Lavinia lavi07 Data 21 decembrie 2014 18:49:26
Problema Fractal Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <stdio.h>

int length(int n) {
    if (n == 0)
        return 0;
    return (1 << (n + 1)) - 1;
}

int fractal(int n, int x, int y) {
    if (n == 0)
        return 0;
    int midx = (1 << (n - 1)) - 1, midy = (1 << (n - 1)) - 1;
    if (x <= midx && y <= midy)
        return fractal(n - 1, y, x);
    else if (x <= midx && y > midy)
        return fractal(n - 1, x - midx, y) + length(n - 1) + 1;
    else if (x > midx && y > midy)
        return fractal(n - 1, x - midx, y - midy) + 2 * length(n - 1) + 2;
    else
        return fractal(n - 1, midy - y, (1 << n) - 1 - x) + 3 * length(n - 1) + 3;
}

int main()
{
    FILE *in, *out;
    in = fopen("fractal.in", "r");
    out = fopen("fractal.out", "w");
    int n, x, y;
    fscanf(in, "%d %d %d", &n, &x, &y);
    fprintf(out, "%d\n", fractal(n, x - 1, y - 1));
    fclose(in);
    fclose(out);
    return 0;
}