Cod sursa(job #1819927)

Utilizator deresurobertoFMI - Deresu Roberto deresuroberto Data 30 noiembrie 2016 23:24:06
Problema Fractal Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <iostream>
#include <cmath>
using namespace std;
int k, x, y;
int c[2][2] = { {1, 4}, {2, 3} }; // |1 4|
int MatLen[26];                   // |2 3|
int fstLen[2][2] = { {0, 3}, {1, 2} };

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

int FindLength(int k, int x, int y)
{
    if (k == 1)
        return fstLen[x - 1][y - 1];

    int dim = pow(k, 2);
    int cadr = c[x > dim / 2][y > dim / 2];

    switch (cadr)
    {
        case 1: return 0 * MatLen[k - 1] + 0 + FindLength(k - 1, y, x);
        case 2: return 1 * MatLen[k - 1] + 1 + FindLength(k - 1, x - dim / 2, y);
        case 3: return 2 * MatLen[k - 1] + 2 + FindLength(k - 1, x - dim / 2, y - dim / 2);
        case 4: return 3 * MatLen[k - 1] + 3 + FindLength(k - 1, dim - y + 1, dim / 2 - x + 1);
    }
}

int main()
{
    fin >> k >> y >> x;

    for (int i = 1; i < k; i++)
        MatLen[i] = 4 * MatLen[i - 1] + 3;

    fout << FindLength(k, x, y);
    return 0;
}