Cod sursa(job #1054596)

Utilizator dorinmoldovanMoldovan Dorin dorinmoldovan Data 13 decembrie 2013 23:24:59
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <iostream>
#include <fstream>

using namespace std;

int hilbert(int order, int x, int y)
{
	if(order == 1)
	{
		if(x == 1 && y == 1) 
			return 0;
		if(x == 1 && y == 2)
			return 1;
		if(x == 2 && y == 2)
			return 2;
		if(x == 2 && y == 1)
			return 3;
	}
	if(order > 1) {
		int dimension = 1;
		for(int i = 0; i < order; i++)
			dimension *= 2;

		// there are four cases 

		// first case

		if((x <= (dimension/2)) && (y <= (dimension/2))) {
			return hilbert(order - 1, y, x);
		}

		// second case

		if((x <= (dimension/2)) && (y > (dimension/2))) {
			return ((dimension / 2) * (dimension / 2) + hilbert(order - 1, x, y - dimension / 2));
		}

		// third case

		if((x > (dimension/2)) && (y > (dimension/2))) {
			return ((dimension / 2) * (dimension / 2) * 2 + hilbert(order - 1, x - dimension / 2, y - dimension / 2));
		}

		// fourth case

		if((x > (dimension/2)) && (y <= (dimension/2))) {
			return ((dimension / 2) * (dimension / 2) * 4 - 1- hilbert(order - 1, y, dimension - x + 1));
		}
	}
}

int main() {
	int x, y, z;
	ifstream fin ("fractal.in");
	fin >> x;
	fin >> y;
	fin >> z;
	fin.close();

	ofstream fout ("fractal.out");
	fout << hilbert(x, y, z);
	fout.close();
	return 0;
}