Cod sursa(job #65231)

Utilizator risenshineAkil Nasser risenshine Data 7 iunie 2007 19:04:40
Problema Fractal Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.93 kb
//  _   _
//  _| |_
// |  _  |
// |_| |_|
//
#include <stdio.h>

int N, x, y;
int a[2][2] = {
	0, 3, 
	1, 2
};

int find(int n, int x, int y) {
	if (n == 1)
		return a[x-1][y-1];
	else {
		if ( y <= 1<<(n-1) )
			if ( x <= 1<<(n-1) )
				// first quadrant, rotate clockwise and reflect
				return find(n-1, y, x);
			else
				// second quadrant, substract from x
				return find( n-1, x - (1<<(n-1)), y ) + (1<<2*(n-1));
		else
			if ( x > 1<<(n-1) )
				// third quadrant, substract from x and y
				return find( n-1, x - (1<<(n-1)), y - (1<<(n-1)) ) + 2 * (1<<2*(n-1));
			else
				// fourth quadrant, substract from y, rotate anticlockwise and reflect 
				return find( n-1, 2*(1<<(n-1)) - y + 1, (1<<(n-1)) - x + 1 ) + 3 * (1<<2*(n-1));
	}
}

int main() {
	freopen("fractal.in", "r", stdin);
	freopen("fractal.out", "w", stdout);
	scanf("%d%d%d", &N, &y, &x);
	printf("%d\n", find(N, x, y));
	return 0;
}