Cod sursa(job #2355619)

Utilizator Vlad_lsc2008Lungu Vlad Vlad_lsc2008 Data 26 februarie 2019 10:45:39
Problema Fractal Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.45 kb
#include <fstream>
#include <iostream>
#define afis  cout<<n<<' '<<xp<<' '<<yp<<' '<<sol<<'\n';
using namespace std;

int n,x,y;

int solve1(int n,int x,int y,int xp,int yp,long long sol);
int solve2(int n,int x,int y,int xp,int yp,long long sol);
int solve3(int n,int x,int y,int xp,int yp,long long sol);
int solve4(int n,int x,int y,int xp,int yp,long long sol);

int solve1(int n,int x,int y,int xp,int yp,long long sol)
{
 //   afis
   // cout<<"SOLVE1\n";
    if(n==0) return sol;
    int l=(1<<n)/2;
    long long ad=(1<<2*n)/4;
    if( yp+l>y)
    {
        if(xp+l>x) return solve2(n-1,x,y,xp,yp,sol);
        else return solve1(n-1,x,y,xp+l,yp,sol+ad);
    }
    else
    {
        if(xp+l>x){
  //              cout<<"AICI2\n";
         return solve4(n-1,x,y,xp+l/2,yp+l+l/2,sol+3*ad);
        }
        else return solve1(n-1,x,y,xp+l,yp+l,sol+2*ad);
    }
}

int solve2(int n,int x,int y,int xp,int yp,long long sol)
{
    //afis
     //cout<<"SOLVE2\n";
    if(n==0) return sol;
    int l=(1<<n)/2;
    long long ad=(1<<2*n)/4;
    if( yp+l>y)
    {
        if(xp+l>x) return solve1(n-1,x,y,xp,yp,sol);
        else return solve3(n-1,x,y,xp+l+l/2,yp+l/2,sol+3*ad);
    }
    else
    {
        if(xp+l>x) return solve2(n-1,x,y,xp,yp+l,sol+ad);
        else return solve2(n-1,x,y,xp+l,yp+l,sol+2*ad);
    }
}

int solve3(int n,int x,int y,int xp,int yp,long long sol)
{
    //afis
     //cout<<"SOLVE3\n";
    if(n==0) return sol;
    int l=(1<<n)/2;
    long long ad=(1<<2*n)/4;
    if( yp-l>=y)
    {
        if(xp-l>=x) return solve3(n-1,x,y,xp-l,yp-l,sol+2*ad);
        else return solve2(n-1,x,y,xp-l/2,yp-l-l/2,sol+3*ad);
    }
    else
    {
        if(xp-l>=x) return solve3(n-1,x,y,xp-l,yp-l/2,sol+ad);
        else return solve4(n-1,x,y,xp,yp,sol);
    }
}

int solve4(int n,int x,int y,int xp,int yp,long long sol)
{
    //afis
     //cout<<"SOLVE4\n";
    if(n==0) return sol;
    int l=(1<<n)/2;
    long long ad=(1<<2*n)/4;
    if( yp-l>=y)
    {
        if(xp-l>=x) return solve4(n-1,x,y,xp-l,yp-l,sol+2*ad);
        else return solve4(n-1,x,y,xp,yp-l,sol+ad);
    }
    else
    {
        if(xp-l>=x) return solve1(n-1,x,y,xp-l-l/2,yp-l/2,sol+3*ad);
        else return solve3(n-1,x,y,xp+l,yp+l,sol);
    }
}

int main()
{
    ifstream t1("fractal.in");
    ofstream t2("fractal.out");
    t1>>n>>x>>y;
    t2<<solve2(n,x,y,1,1,0);
    t1.close();
    t2.close();
    return 0;
}