Cod sursa(job #576729)

Utilizator mihaipopa12Popa Mihai mihaipopa12 Data 9 aprilie 2011 14:52:45
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include<stdio.h>

#define cond1 (x>(1<<(p-1)))
#define cond2 (y>(1<<(p-1)))

FILE*f=fopen("fractal.in","r");
FILE*g=fopen("fractal.out","w");

int k,y,x;

int fractal(int x,int y,int p){
	
	//reduc la ordin p - 1
	
	if ( p == 1 ){
		if ( x == 1 && y == 2 )
			return 3;
		if ( x == 2 && y == 1 )
			return 1;
		if ( x == 2 && y == 2 )
			return 2;
		return 0;
	}
	
	int r = 0;
	
	if ( cond1 && cond2 ){
		r = fractal(x-(1<<(p-1)),y-(1<<(p-1)),p-1) ;
		return (r + (1<<(p+p-1)));
	}
	
	if ( cond1 && !cond2 ){
		r = fractal(x-(1<<(p-1)),y,p-1);
		return (r+(1<<(p+p-2)));
	}
	
	if ( !cond1 && cond2 ){
		r = fractal((1<<p)-y+1,x,p-1);
		return  (4 * (1<<(p+p-2)) - 1 - r);
	}
	
	if ( !cond1 && !cond2 ){
		r = fractal(y,(1<<(p-1)) - x + 1,p-1);
		return ((1<<(p+p-2)) - 1 - r);
	}
	
}

int main () {
	
	fscanf(f,"%d %d %d",&k,&y,&x);
	
	fprintf(g,"%d\n",fractal(x,y,k));
	
	fclose(f);
	fclose(g);
	
	return 0;
}