#include <stdio.h>
const char file_in[]="embald.in";
const char file_out[]="embald.out";
int librex,librey;
int size;
char floor[128][128];
char cuadrante[8];
char load(void);
void save(void);
char ret_bald(int xc,int yc);
char put_bald(int cx,int cy,char forma);
void gen_cuadrante(void);
void rellenar(void);
void embaldosa(int recsize,int x,int y,char nivel,char cuaduse);
void main(void)
{
char c,n=0;
if (!load())
return;
for (c=0;c<8;c++)
if (size&(1<<c)) n++;
if (n>1) return;
if (size>4)
{
gen_cuadrante();
embaldosa(size,0,0,0,1);
}
if (size>1)
rellenar();
save();
}
char load(void)
{
FILE *f;
f=fopen(file_in,"rt");
if (!f) return 0;
fscanf(f,"%d %d %d",&size,&librey,&librex);
librex--;librey--;
floor[librex][librey]='0';
fclose(f);
return 1;
}
void save(void)
{
FILE *f;
int cx,cy;
f=fopen(file_out,"wt");
for (cy=0;cy<size;cy++)
{
for (cx=0;cx<size;cx++)
fprintf(f,"%c",floor[cx][cy]);
fprintf(f,"\n");
}
fclose(f);
}
char put_bald(int cx,int cy,char forma)
{
switch(forma)
{
case 1:
if (floor[cx][cy] || floor[cx][cy+1] || floor[cx+1][cy+1]) return 0;
floor[cx][cy]='1';
floor[cx][cy+1]='1';
floor[cx+1][cy+1]='1';
break;
case 2:
if (floor[cx+1][cy] || floor[cx][cy+1] || floor[cx+1][cy+1]) return 0;
floor[cx+1][cy]='2';
floor[cx][cy+1]='2';
floor[cx+1][cy+1]='2';
break;
case 3:
if (floor[cx][cy] || floor[cx][cy+1] || floor[cx+1][cy]) return 0;
floor[cx][cy]='3';
floor[cx][cy+1]='3';
floor[cx+1][cy]='3';
break;
case 4:
if (floor[cx][cy] || floor[cx+1][cy+1] || floor[cx+1][cy]) return 0;
floor[cx][cy]='4';
floor[cx+1][cy+1]='4';
floor[cx+1][cy]='4';
break;
}
return 1;
}
char ret_bald(int xc,int yc)
{
if (floor[xc+1][yc])
return 1;
if (floor[xc][yc])
return 2;
if (floor[xc+1][yc+1])
return 3;
if (floor[xc][yc+1])
return 4;
return 0;
}
void gen_cuadrante(void)
{
int i;
char c;
int tcuad;
int nx,ny;
nx=librex;
ny=librey;
for (c=1;c<8;c++)
if (size&(1<<c)) break;
for (i=c;i>1;i--)
{
tcuad=(1<<i)/2;
if (nx<tcuad && ny<tcuad)
cuadrante[c-i]=2;
else
if (nx<tcuad && ny>=tcuad)
cuadrante[c-i]=4;
else
if (nx>=tcuad && ny<tcuad)
cuadrante[c-i]=1;
else
if (nx>=tcuad && ny>=tcuad)
cuadrante[c-i]=3;
if (cuadrante[c-i]&1) nx-=tcuad;
if (cuadrante[c-i]>=3) ny-=tcuad;
}
}
void embaldosa(int recsize,int x,int y,char nivel,char cuaduse)
{
char cuad,crcuad;
if (recsize==4) return;
if (cuaduse)
{
cuad=cuadrante[nivel];
put_bald( x+(recsize/2)-1, y+(recsize/2)-1, cuad);
}
else
{
if (floor[x+recsize-1][y]) cuad=1; else
if (floor[x][y]) cuad=2; else
if (floor[x+recsize-1][y+recsize-1]) cuad=3; else
if (floor[x][y+recsize-1]) cuad=4;
put_bald( x+(recsize/2)-1, y+(recsize/2)-1, cuad);
cuad=0;
}
if (cuad==1) crcuad=1; else crcuad=0;
embaldosa(recsize/2,x+(recsize/2),y,nivel+1,crcuad);
if (cuad==2) crcuad=1; else crcuad=0;
embaldosa(recsize/2,x,y,nivel+1,crcuad);
if (cuad==3) crcuad=1; else crcuad=0;
embaldosa(recsize/2,x+(recsize/2),y+(recsize/2),nivel+1,crcuad);
if (cuad==4) crcuad=1; else crcuad=0;
embaldosa(recsize/2,x,y+(recsize/2),nivel+1,crcuad);
}
void rellenar(void)
{
int xc, yc;
for (yc=0;yc<size;yc+=4)
for (xc=0;xc<size;xc+=4)
{
put_bald(xc,yc,ret_bald(xc,yc));
put_bald(xc+2,yc,ret_bald(xc+2,yc));
put_bald(xc,yc+2,ret_bald(xc,yc+2));
put_bald(xc+2,yc+2,ret_bald(xc+2,yc+2));
put_bald(xc+1,yc+1,ret_bald(xc+1,yc+1));
put_bald(xc,yc,ret_bald(xc,yc));
put_bald(xc+2,yc,ret_bald(xc+2,yc));
put_bald(xc,yc+2,ret_bald(xc,yc+2));
put_bald(xc+2,yc+2,ret_bald(xc+2,yc+2));
}
} |