Thread: Help solving a maze!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Wink Help solving a maze!

    Hi, i have a question i need to create a program that creates a maze which i can design my self i used this design:
    @@@@@@
    @S@xxxx@
    @x@xxxx@ @=walls
    @xxxxxxx@ S=Start
    @xxxxxxE@ E=End
    @@@@@@ x=emptyspace or ' ' in matrix

    and i have all those values on a matrix, but can anyone give me an insight on how can i solve this maze, i mean how should the north.south,east,west, functions be more less, because really i have no idea. thank you very much in advance! hope someone can help
    Last edited by cosio55; 11-09-2010 at 09:15 PM. Reason: maze not displaying correctly

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Look up the A* (A star) algorithm on Google.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    What i have so far

    It just goes around in a square, any idea how to make it move all around the maze?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define COLMAX 8
    #define RENMAX 8
    
    void Laberinto(char Lab[COLMAX][RENMAX]);
    void BuscarEntrada(char Lab[COLMAX][RENMAX], int PosE[1][1]);
    void BuscarSalida(char Lab[COLMAX][RENMAX], int PosE[1][1]);
    
    main()
    {
    	char Lab[COLMAX][RENMAX];
    	int PosE[1][1];
    	Laberinto(Lab);
    	BuscarEntrada(Lab, PosE);
    	BuscarSalida(Lab, PosE);
    }
    
    void Laberinto(char Lab[COLMAX][RENMAX])
    {
    	int i,j;
    	for (i=0,j=0; j<8; j++)
    		Lab[i][j]='@';
    	Lab[1][0]='@';
    	Lab[1][1]='E';
    	Lab[1][2]=' ';
    	Lab[1][3]=' ';
    	Lab[1][4]=' ';
    	Lab[1][5]=' ';
    	Lab[1][6]=' ';
    	Lab[1][7]='@';
    	Lab[2][0]='@';
    	Lab[2][1]=' ';
    	Lab[2][2]=' ';
    	Lab[2][3]=' ';
    	Lab[2][4]=' ';
    	Lab[2][5]=' ';
    	Lab[2][6]=' ';
    	Lab[2][7]='@';
    	Lab[3][0]='@';
    	Lab[3][1]=' ';
    	Lab[3][2]='@';
    	Lab[3][3]='@';
    	Lab[3][4]='@';
    	Lab[3][5]=' ';
    	Lab[3][6]=' ';
    	Lab[3][7]='@';
    	Lab[4][0]='@';
    	Lab[4][1]=' ';
    	Lab[4][2]=' ';
    	Lab[4][3]=' ';
    	Lab[4][4]=' ';
    	Lab[4][5]=' ';
    	Lab[4][6]=' ';
    	Lab[4][7]='@';
    	Lab[5][0]='@';
    	Lab[5][1]=' ';
    	Lab[5][2]=' ';
    	Lab[5][3]=' ';
    	Lab[5][4]=' ';
    	Lab[5][5]=' ';
    	Lab[5][6]=' ';
    	Lab[5][7]='@';
    	Lab[6][0]='@';
    	Lab[6][1]=' ';
    	Lab[6][2]=' ';
    	Lab[6][3]=' ';
    	Lab[6][4]=' ';
    	Lab[6][5]=' ';
    	Lab[6][6]=' ';
    	Lab[6][7]='@';
    	for (i=7,j=0; j<8; j++)
    		Lab[i][j]='@';
    	for (i=0; i<8; i++) {
    		for (j=0; j<8; j++) {
    			printf("%c",Lab[i][j]);
    		}
    		printf("\n");
    	}
    }
    
    void BuscarEntrada(char Lab[COLMAX][RENMAX], int PosE[1][1])
    {
    	int i,j;
    	for (i=0; i<8; i++) {
    		for (j=0; j<8; j++) {
    			if (Lab[i][j]=='E') {
    				PosE[0][0]=i;
    				PosE[1][0]=j;
    			}
    		}
    	}
    }
    
    void BuscarSalida(char Lab[COLMAX][RENMAX], int PosE[1][1])
    {
    	while (Lab[PosE[0][0]][PosE[1][0]]!='@') {
    		PosE[1][0]++;
    		printf("%d",PosE[1][0]);
    		if(Lab[PosE[0][0]][PosE[1][0]]=='S')
    			printf("Ganaste");
    	}
    	PosE[1][0]--;
    	while (Lab[PosE[0][0]][PosE[1][0]]!='@') {
    		PosE[0][0]++;
    		printf("%d",PosE[0][0]);
    		if(Lab[PosE[0][0]][PosE[1][0]]=='S')
    			printf("Ganaste");
    	}
    	PosE[0][0]--;
    	while (Lab[PosE[0][0]][PosE[1][0]]!='@') {
    		PosE[1][0]--;
    		printf("%d",PosE[1][0]);
    		if(Lab[PosE[0][0]][PosE[1][0]]=='S')
    			printf("Ganaste");
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solving maze using rec'
    By gavra in forum C Programming
    Replies: 14
    Last Post: 07-13-2008, 09:20 AM
  2. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 01:52 AM
  3. Maze Solving Cont....PLZ
    By jonnybgood in forum C Programming
    Replies: 30
    Last Post: 11-11-2005, 04:00 AM
  4. Solving A Maze Using C Language!!!!
    By jonnybgood in forum C Programming
    Replies: 6
    Last Post: 11-08-2005, 12:15 PM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM

Tags for this Thread