Cod sursa(job #230007)

Utilizator marinMari n marin Data 12 decembrie 2008 17:16:59
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <stdio.h>

int n,m,xx,yy,tot,k,x,y;

void rec(int x, int y, int k){
	if (k==1) {
		if ((x==1)&&(y==1))
			tot += 0;
		if  ((x==1)&&(y==2))
			tot += 3;
		if ((x==2)&&(y==1))
			tot +=1;
		if ((x==2)&&(y==2))
			tot +=2;
		return;
	}
	
	
	m = 1<<(k-1);
	if ((x>m)&&(y<=m)) {
		tot+=(m*m);
		rec(x-m,y,k-1);
	} else
	if ((x>m)&&(y>m)) {
		tot+=(2*m*m);
		rec(x-m,y-m,k-1);
	} else
	if ((x<=m)&&(y<=m)) {
		xx = x;
		yy = y;
		y = m-xx+1;
		x = yy;
		y = m-y+1;
		rec(x,y,k-1);
	} else {
	  y = y-m;
	  xx = x;
	  yy = y;
	  x = m-yy+1;
	  y = xx;
	  y = m-y+1;
	  tot += (3*m*m);
	  rec(x,y,k-1);
	}
}

int main(){
	FILE *f = fopen("fractal.in", "r");
	fscanf(f,"%d %d %d", &k, &y, &x);
	fclose(f);
	rec(x,y,k);
	FILE *g = fopen("fractal.out","w");
	fprintf(g, "%d", tot);
	fclose(g);
	return 0;
}