Cod sursa(job #2355706)

Utilizator Vlad_lsc2008Lungu Vlad Vlad_lsc2008 Data 26 februarie 2019 11:41:02
Problema Fractal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.52 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){
        return solve4(n-1,x,y,xp+l-1,yp+l+l-1,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-1,yp+l-1,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+1,yp-l-l+1,sol+3*ad);
    }
    else
    {
        if(xp-l>=x) return solve3(n-1,x,y,xp-l,yp,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+1,yp-l+1,sol+3*ad);
        else return solve3(n-1,x,y,xp,yp,sol);
    }
}

int main()
{
    ifstream t1("fractal.in");
    ofstream t2("fractal.out");
    t1>>n>>x>>y;
    t2<<solve2(n,x,y,1,1,0);
    //n=3;
    int i,j;
    //for(i=1;i<=1<<n;i++)
      //  for(j=1;j<=1<<n;j++) cout<<solve2(n,j,i,1,1,0)<<'\n';
    t1.close();
    t2.close();
    return 0;
}