Cod sursa(job #480613)

Utilizator budulaiSuman Dinu budulai Data 28 august 2010 20:36:34
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <stdio.h>
#include <stdlib.h>

int lungime(int n)
{
	int rez=3;
	if(n<=1) return 3;
	while(n>1)
	{
	rez = rez*4+3;
	n--;
	}
	return rez;
}

int calc(int k, int x, int y)
{
	//printf("%d %d %d \n",k,x,y);
	int i,n=2, l;
	if(k==1)
	{
		if(x==1 && y==2)
			return 1;
		if(x==2 && y==2)
			return 2;
		if(x==2 && y==1)
			return 3;
		return 0;
	}
	else
	{
		for(i=1;i<k;i++)
			n=n*2;
		int mij = n/2;
	/*	if(x==mij || x==mij+1 || y==mij || y==mij+1)
		{
		
		}
		else*/
		//{
			//afla care patrat
			l=lungime(k-1)+1;
			if(x<=mij && y <=mij) //stinga sus
				return calc(k-1,y,x);
			else
			if(x<=mij && y > mij) //stinga jos
				return l + calc(k-1,x,y-mij); //nu incrucisate
			else
			if(x>mij && y>mij) //dreapta jos
				return 2*l + calc(k-1,x-mij,y-mij); //nu incrucisate
			else
				return 3*l + calc(k-1,mij-y+1,2*mij-x+1); //incrucisate
		//}
		
	}
	return 0;
}

int main()
{
	int k,x,y;
	freopen("fractal.in", "r", stdin);
	freopen("fractal.out", "w", stdout);
	scanf("%d",&k);
	scanf("%d",&x);
	scanf("%d",&y);

	printf("%d",calc(k,x,y));

	return 0;
}