Ayuda!!!

Evelia
16 de Junio del 2004
Hola, necesito un programa en C de un laberinto. Si alguien puede ayudarme, se los agradeceré demasiado.
GRACIAS!!!

neo20
16 de Junio del 2004
Hola , te envio un codigo de hace mas de 2 años , cuando era un pokito inexperto en el tema, veras ke no sta bien pulido ni mucho menos , pero funciona!!
al final pongo un fichero .txt de prueba.

#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100
typedef struct sdesplazamiento
{
int vert;
int horiz;
}desplazamientos;
typedef struct sElem
{
int fil;
int col;
int dir;
} TElem;

typedef struct sStack
{
int top;
TElem e[MAX_STACK_SIZE];
} TStack;

/*---------------------------------------PILA----------------------------------------------*/

void initstack(TStack *pStack)
{
pStack->top=-1;
}

void push (TStack *pStack , TElem Tpos)
{
pStack->e[++(pStack->top)]=Tpos;
}

TElem pop(TStack *pStack)
{
TElem op;
op=pStack->e[(pStack->top)--];
return op;
}

/*------------------------------------FIN PILA--------------------------------------------------*/


/*-----------------------------------LABERINTO----------------------------------------------------------------*/
void laberinto (int dir, int *lab ,TElem *mat ,int i,int j,desplazamientos *mov,TStack *pStack,int *visited)
{
TElem s;
TElem Tpos,Tp;
int k,m;
mat =(TElem*)malloc(MAX_STACK_SIZE*sizeof(TElem));
if (i==10&&j==10)//si ya hemos llegado a la salida;
{
Tpos.fil=i;
Tpos.col=j;
Tpos.dir=dir ;
push(pStack,Tpos);
k=0;
while(pStack->top!=-1)
{
s=pop(pStack);
*(mat+k)=s;
k++;
}

for(m=k-1;m>=0;m--) //solucion si es que hay camino;
{
printf("[%d,%d] ",(mat+m)->fil,(mat+m)->col);
}
}
else if ((*(lab+i*12+j)==1)||(*(visited+i*12+j)==1))//si esta en una pared o bien si esa posicion ya esta ya visitada;
{
Tp=pop(pStack);
i=Tp.fil;
j=Tp.col;
dir=Tp.dir ;
dir=dir+1;
*(visited+i*12+j)=0;
laberinto(dir,lab,mat,i,j,mov,pStack,visited);
}
else if (dir>=7) //si no puede ir a ningun otro sitio;
{
if((i!=1)&&(j!=1))//vuelve para ver otro posible camino;
{
Tp=pop(pStack);
i=Tp.fil;
j=Tp.col;
dir=Tp.dir ;
dir=dir+1;
*(visited+i*12+j)=0;
laberinto(dir,lab,mat,i,j,mov,pStack,visited);
}
else //si no hay camino;
{
printf("No hay camino");
}
}
else //si encuentra un camino transitable;
{
*(visited+i*12+j)=1;
Tpos.fil=i;
Tpos.col=j;
Tpos.dir=dir ;
push(pStack,Tpos);
i=i+mov[dir].vert;
j=j+mov[dir].horiz;
laberinto(0 , lab , mat , i, j,mov,pStack,visited);
}
}
/*-------------------------------------FIN LABERINTO------------------------------------------------------------*/

/*-----------------------------------------MAIN------------------------------------------------------------------*/
main()
{
FILE *fichero,*fout;
char *aux;
int i,j,c,*lab;
int flag=0;
int mat,dir=0;
int *visited;
TStack pStack;
/*------------------------MOVIMIENTOS------------------*/
desplazamientos *mov;
visited=(int*)malloc((12*12)*sizeof(int));
lab=(int*)malloc((12*12)*sizeof(int));
mov=(desplazamientos*)malloc(8*sizeof(desplazamientos));
mov[0].vert=-1;
mov[0].horiz=0;
mov[1].vert=-1;
mov[1].horiz=1;
mov[2].vert=0;
mov[2].horiz=1;
mov[3].vert=1;
mov[3].horiz=1;
mov[4].vert=1;
mov[4].horiz=0;
mov[5].vert=1;
mov[5].horiz=-1;
mov[6].vert=0;
mov[6].horiz=-1;
mov[7].vert=-1;
mov[7].horiz=-1;
/*-----------------PONER UNOS----------------*/
i=0;
for (j=0;j<12;j++)
{
*(lab+i*12+j)=1;
}

i=11;
for (j=0;j<12;j++)
{
*(lab+i*12+j)=1;
}
j=0;
for (i=1 ; i<11 ;i++)
{
*(lab+i*12+j)=1;
}
j=11;
for (i=1 ; i<11 ;i++)
{
*(lab+i*12+j)=1;
}
/*---------------------------fichero----------------------------*/
if((fichero=fopen("maze.txt","r"))==NULL)
{
printf("error al abrir el ficheron");
exit(1);
}
if ((fout = fopen ("salida.txt", "w")) == NULL)
{
printf ("Error de creación del ficheron");
exit (1);
}
i=0;
j=0;
while (c!= EOF)
{
c=getc(fichero);
fprintf(fout,"%c%c",' ',c);
}
fclose(fichero);
fclose(fout);

if((fichero=fopen("salida.txt","r"))==NULL)
{
printf("error al abrir el ficheron");
exit(1);
}

while (!feof(fichero))
{
for(i=1;i<11;i++)
{
for (j=1;j<11;j++)
{
if ( fscanf(fichero,"%c%d",&aux,(lab+i*12+j)) ==EOF)
{
flag = 1;
break;
}
}
if (flag == 1) break;
}
}

printf("--------------------------EL LABERINTO ES EL SIGUIENTE:-------------------------n");
for (i=0;i<12;i++)
{
for(j=0;j<12;j++)
{
printf("%d",*(lab+i*12+j));
}
printf("n");
}
/*--------inicializar matriz de las posiciones visitadas----*/
for (i=0;i<12;i++)
{
for(j=0;j<12;j++)
{
*(visited+i*12+j)=0;
}
}
/*-----------------------------------------------------------*/
printf("-------------------Y LAS COORDENADAS DEL CAMINO A LA SALIDA:--------------------n");
initstack(&pStack);
i=1;
j=1;
laberinto(dir , lab ,&mat , i, j,mov,&pStack,visited);
printf("n");
}

FICHERO EJEMPLO "maze.txt"

0000000001
1111111110
1111110001
1110001111
1001111111
0111111111
1011111111
0111111111
0101010011
10101011002

Rev
16 de Junio del 2004
Tal vez esto te sirva, es algo simple sin tiempo ni nada pero hace lo del laberinto, y el usuario busca la salida:

#include <stdio.h>
#include <conio.h>
void PintarPantalla(int matriz[][70]);

const char paredes[]={'²','.','þ',' '};
#define ESC 27

void main(void)
{
int laberinto[20][70]=
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,1,1,1,1,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,0,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1,1,1,1,0,0,1,0,1,1,0,1,1,0,1,0,0,1,0,1,1,1,0,1,1,0,1,1,1,0,1,0,0,1,0,0,0,1,0,1,1,0,1,0,1,0},
{0,1,1,0,1,1,1,1,0,1,0,1,1,1,0,1,0,0,1,0,0,1,1,1,1,1,0,1,0,1,0,0,0,1,1,1,0,0,1,1,1,1,0,0,0,1,1,0,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0,1,0},
{0,0,1,1,0,0,0,1,1,0,1,1,0,1,0,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,1,0,1,1,0,1,1,1,0,0,0,1,1,0,1,1,1,0,1,1,1,0,1,1,0,1,0,1,1,0,1,1,1,0,1,0,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};//char laberinto[20][70]
short int x=8,y=0;

PintarPantalla(laberinto);
gotoxy(y+1,x+5);

char mov;
short int win=0;

_setcursortype(0);

do
{
mov=getch();

if(mov==49 || mov==50 || mov==51 || mov==53)
{
short int temp=0,sigue=0;
switch(mov)
{
case 49:
temp=y-1;
if(laberinto[x][temp]!=0 && x>=1)
sigue=1;
break;
case 50:
temp=x+1;
if(laberinto[temp][y]!=0)
sigue=1;
break;
case 51:
temp=y+1;
if(laberinto[x][temp]!=0 && x>=1)
sigue=1;
break;
case 53:
temp=x-1;
if(laberinto[temp][y]!=0)
sigue=1;
break;
}//switch(mov)

if(sigue)
{
gotoxy(y+1,x+5); textcolor(15); cprintf("%c",paredes[1]);

switch(mov)
{
case 49:
y--; break;
case 50:
x++; break;
case 51:
y++; break;
case 53:
x--; break;
}//switch(mov)
}//if(sigue)
}//if(mov==49 || mov==50 || mov==51 || mov==53)

gotoxy(y+1,x+5); textcolor(4); cprintf("%c",paredes[2]);

if(laberinto[x][y]==3)
{
mov=ESC;
win=1;
}

}while(mov!=ESC);

if(win)
{ gotoxy(42,30); printf("­ Ganaste !"); }
else
{ gotoxy(42,30); printf("­ Perdiste !"); }

getch();
}

void PintarPantalla(int matriz[][70])
{
textcolor(15);
clrscr();
printf("nnttt Laberintonn");

for(short int a=0;a<=19;a++)
{
for(short int b=0;b<=69;b++)
{
if(matriz[a][b]!=2)
{ textcolor(15); cprintf("%c",paredes[matriz[a][b]]); }
else
{ textcolor(4); cprintf("%c",paredes[matriz[a][b]]); }
}
printf("n");
}

printf("nn Puedes moverte a trav‚s del laberinto con las teclas:");
printf("nnt -----------------------");
printf("nt | Bloque Numerico |");
printf("nt -----------------------");
printf("nnt 5");
printf("nnt Arriba");
printf("nnt 1 2 3");
printf("nnt Derecha Abajo Izquierda");
}