Cod sursa(job #178015)

Utilizator varuvasiTofan Vasile varuvasi Data 13 aprilie 2008 23:46:23
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.2 kb
#include <stdio.h>

int K, xst, yst, nrot, res;
int v[4] = {1,2,3,4};
int pow2[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
int pow4[] = {0,1,4,16,64,256,1024,4096,16384,65536,262144,1048576,4194304,16777216,67108864,268435456};

FILE *fin = fopen("fractal.in", "rt"), *fout = fopen("fractal.out", "wt");
void calcord(int rot)
{
	if (rot == 0) v[0] = 1, v[1] = 2, v[2] = 3, v[3] = 4;
	if (rot == 1) v[0] = 3, v[1] = 2, v[2] = 1, v[3] = 4;
	if (rot == 2) v[0] = 3, v[1] = 4, v[2] = 1, v[3] = 2;
	if (rot == 3) v[0] = 1, v[1] = 4, v[2] = 3, v[3] = 2;
/*	if (cad == 2)
	{
		if (rot == 0) v[0] = 1, v[1] = 2, v[2] = 3, v[3] = 4;
		if (rot == 1) v[0] = 3, v[1] = 2, v[2] = 1, v[3] = 4;
		if (rot == 2) v[0] = 3, v[1] = 4, v[2] = 1, v[3] = 2;
		if (rot == 3) v[0] = 1, v[1] = 4, v[2] = 3, v[3] = 2;
	}*/
}

void calcnrot(int cad)
{
//	fprintf(fout, "%d\n", cad);
	if (nrot == 0)
	{
		if (cad == 1) nrot = (3 + nrot) % 4;
		else if (cad == 4) nrot = (++nrot) % 4;
		return;
	}
	if (nrot == 1)
	{
		if (cad == 4) nrot = (3 + nrot) % 4;
		else if (cad == 3) nrot = (++nrot) % 4;
		return;
	}
	if (nrot == 2)
	{
		if (cad == 3) nrot = (3 + nrot) % 4;
		else if (cad == 2) nrot = (++nrot) % 4;
		return;
	}
	if (nrot == 3)
	{
		if (cad == 2) nrot = (3 + nrot) % 4;
		else if (cad == 1) nrot = (++nrot) % 4;
		return;
	}
}

void rec(int k, int x, int y)
{
	if (!k) return;
//	fprintf(fout, "%d %d %d %d %d\n", k, x, y, nrot, res);
//	fprintf(fout, "%d %d %d %d\n", v[0], v[1], v[2], v[3]);
	if (x <= pow2[k - 1] && y <= pow2[k - 1])
	{
		//cadranul I
		res += (v[0] - 1) * pow4[k];
		calcnrot(1);
		calcord(nrot);
		rec(k - 1, x, y);
	}
	if (x > pow2[k - 1] && y <= pow2[k - 1])
	{
		res += (v[1] - 1) * pow4[k];
		calcnrot(2);
		calcord(nrot);
		rec(k - 1, x - pow2[k - 1], y);
	}
	if (x > pow2[k - 1] && y > pow2[k - 1])
	{
		res += (v[2] - 1) * pow4[k];
		calcnrot(3);
		calcord(nrot);
		rec(k - 1, x - pow2[k - 1], y - pow2[k - 1]);
	}
	if (x <= pow2[k - 1] && y > pow2[k - 1])
	{
		res += (v[3] - 1) * pow4[k];
		calcnrot(4);
		calcord(nrot);
		rec(k - 1, x, y - pow2[k-1]);
	}
}

int main()
{
	fscanf(fin, "%d %d %d", &K, &yst, &xst);
	rec(K, xst, yst);
	fprintf(fout, "%d", res);
	fclose(fin), fclose(fout);
	return 0;
}