Cod sursa(job #789949)

Utilizator andrei.sfrentSfrent Andrei andrei.sfrent Data 19 septembrie 2012 22:21:57
Problema Fractal Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#include <iostream>

using namespace std;

ifstream fi("fractal.in");
ofstream fo("fractal.out");

int pow2[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
8192, 16384, 32768};

int K, X, Y;

void read()
{
  fi >> K >> X >> Y;
}

int cadran(int k, int x, int y)
{
  int h = pow2[k] / 2;
  if(x <= h && y <= h) return 0;
  if(x <= h && y > h) return 1;
  if(x > h && y > h) return 2;
  if(x > h && y <= h) return 3;

  return -1;
}

int T[16];
void tsteps()
{
  T[0] = 0;
  for(int i = 1; i < 16; i++)
    T[i] = 3 * T[i - 1] + 3;
}

int pasi(int k, int x, int y)
{
  if(k == 0)
    return 0;

  int c = cadran(k, x, y);
  int h = pow2[k] / 2;

  switch(c)
  {
    case 0:
      return pasi(k - 1, y, x);
    case 1:
      return T[k - 1] + 1 + pasi(k - 1, x, y - h);
    case 2:
      return 2 * (T[k - 1] + 1) + pasi(k - 1, x - h, y - h);
    case 3:
      return 3 * (T[k - 1] + 1) + pasi(k - 1, h - y + 1, 2 * h - x + 1);
  }

  return -1;
}

int main()
{
  read();
  tsteps();
  fo << pasi(K, X, Y) << endl;
  return 0;
}