Cod sursa(job #1590608)

Utilizator PraetorGrigorosoaia Florin Praetor Data 5 februarie 2016 13:01:13
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include<fstream>

using namespace std;

FILE*in;
ofstream out("fractal.out");

int ordin_initial;
int x_initial, y_initial; // coordonatele punctului cautat

int POWER(int baza, unsigned int putere)
{
    if (putere == 0)
        return 1;

    return baza*POWER(baza, putere-1);
}

void read()
{
    in=fopen("fractal.in", "r");

    fscanf(in, "%d%d%d", &ordin_initial, &x_initial, &y_initial);
}

int DET(int ordin, int x, int y)
{
    if (ordin == 1)
        return (2-x)*(y-1)+(x-1)*(-y+4);

    int half=POWER(2, ordin-1);
    int newX, newY;
    int zone;

    if ((x <= half) && (y <= half)) // zona 0
    {
        zone=0;
        newX=y;
        newY=x;
    }
    else if ((x <= half) && (y > half)) // zona 1(sub zona 0)
    {
        zone=1;
        newX=x;
        newY=y-half;
    }
    else if ((x > half) && (y > half)) // zona 2(in dreapta zonei 1)
    {
        zone=2;
        newX=x-half;
        newY=y-half;
    }
    else // zona 3(deasupra zonei 2 si in dreapta zonei 0)
    {
        zone=3;
        newX=half-y+1;
        newY=2*half-x+1;
    }

    return zone*POWER(4, ordin-1)+DET(ordin-1, newX, newY);
}

int main()
{
    read();
    out<<DET(ordin_initial, x_initial, y_initial);

    return 0;
}