#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#define rando( min, max ) ((rand() % (int)(((max)+1) - (min))) + (min))
int w=0, z=0, i=0, j=0, limit=100;
void pon(int lab[][10])
{
clrscr();
for(int t=0; t<10;t++){
for(int r=0; r<10;r++)
printf("%c",lab[t][r]>0? lab[t][r]>1? lab[t][r]>2? '@': '*': '_' :'|');
printf("\n");
}
}
int esta(int lab [][10],int &x, int &y){
int repa=0;
if(lab[x][y+1]==1){ i=x; j=++y; repa=1;}
if(lab[x+1][y]==1){ i=++x; j=y; repa=1;}
if(lab[x][y-1]==1){ i=x; j=--y; repa=1;}
if(lab[x-1][y]==1){ i=--x; j=y; repa=1;}
return repa;
}
void rebusca(int lab[][10])
{
int x=0, y=0, tri=0, poas=0;
for(;;){
if(lab[x][y+1]>=2 && tri!=3){ ++y; tri=1; if(esta(lab, x, y)) break; else poas++; }
if(lab[x+1][y]>=2 && tri!=4){ ++x; tri=2; if(esta(lab, x, y)) break; else poas++; }
if(lab[x][y-1]>=2 && tri!=1){ --y; tri=3; if(esta(lab, x, y)) break; else poas++; }
if(lab[x-1][y]>=2 && tri!=2){ --x; tri=4; if(esta(lab, x, y)) break; else poas++; }
if(poas>=5){ poas=0; limit+=30;}
}
}
void fill(int lab[][10])
{
int x=1,cox, tesk=0;
for(i=0;i<10;i++) for(j=0;j<10;j++) lab[i][j]=0;
lab[0][0]=1;
for(i=0,j=0;;) {
switch(rando(4,13)){
case 5:
if(j+x<10 && tesk!=3 ){
for(cox=j+x;j<=cox;j++) lab[i][j]=1;
pon(lab); tesk=1;
} break; //der
case 7:
if(i+x<10 && tesk!=4 ){
for(cox=i+x;i<=cox;i++) lab[i][j]=1;
pon(lab); tesk=2;
} break; //abajo
case 9:
if(j-x>0 && tesk!=1 && lab[i][j-1]==0){
for(cox=j-x;j>=cox;j--) lab[i][j]=1;
pon(lab); tesk=3;
} break; //izq
case 11:
if(i-x>0 && tesk!=2 && lab[i-1][j]==0){
for(cox=i-x;i>=cox;i--) lab[i][j]=1;
pon(lab); tesk=4;
} break; //arriba
}
if(i>=9 || j>=9){ w=i; z=j; if(i>9) w=9; if(j>9) z=9; i=j=0; break;}
}
pon(lab);
}
void res(int lab[][10])
{
static x=0, cast=0;;
if(j+1<10 && x!=2){ // derecha
if(lab[i][j+1]>=1){ lab[i][j]=2; lab[i][++j]=3;
pon(lab); x=1; cast+=2; }
}
if(i+1<10 && x!=4){ //abajo
if(lab[i+1][j]>=1){ lab[i][j]=2; lab[++i][j]=3;
pon(lab); x=3; cast+=2;}
}
if(j-1>=0 && x!=1){ //izquierda
if(lab[i][j-1]>=1){ lab[i][j]=2; lab[i][--j]=3;
pon(lab); x=2; cast+=2; }
}
if(i-1>=0 && x!=3){ //arriba
if(lab[i-1][j]>=1){ lab[i][j]=2; lab[--i][j]=3 ;
pon(lab); x=4; cast+=2; }
}
if(cast>limit){ cast=0; rebusca(lab); }
if(i==w && j==z){ getch(); exit(0);}
res(lab);
}
void main()
{
int lab[10][10];
srand((unsigned)time(NULL));
clrscr();
printf("Este programa podria no funcionar, es necesesario ejecutarlo varias veces\n");
printf("el margen de error es de 3 por cada 20 ejecuciones\n"); getch();
fill(lab);
res(lab);
getch();
}
Comentarios sobre la versión: Versión 1 (1)