Cod sursa(job #1979504)

Utilizator Mihai145Oprea Mihai Adrian Mihai145 Data 10 mai 2017 18:56:30
Problema Fractal Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;

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

int k, x, y, cost;

void divide(int k, int x, int y, int a, int b, int l)
{
    if(k==1)
    {
        if(x==a && y==b)
            return;
        if(x==a+1 && y==b)
            cost+=1;
        if(x==a+1 && y==b+1)
            cost+=2;
        if(x==a && y==b+1)
            cost+=3;
    }
    else
    {
        if(a+l/2-1>=x && b+l/2-1>=y)
                divide(k-1,x,y,a,b,l/2);
        else
            if(a+l/2<=x && b+l/2-1>=y)
                {
                    cost+=(l/2)*(l/2);
                    divide(k-1,x,y,a+l/2,b,l/2);
                }
            else
                    if(a+l/2<=x && b+l/2<=y)
                        {
                            cost+=2*(l/2)*(l/2);
                            divide(k-1,x,y,a+l/2,b+l/2,l/2);
                        }
                        else
                            {
                                cost+=3*(l/2)*(l/2);
                                divide(k-1,x,y,a,b+l/2,l/2);
                            }
    }
}

int main()
{
    fin>>k>>x>>y;
    int i, j, l;
    l=(int)powl(2,k);
    divide(k,x,y,1,1,l);
    fout<<cost<<'\n';
    return 0;
}