1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Maze Solver with right-hand method

Discussion in 'C++' started by anon_90, Oct 3, 2011.

  1. anon_90

    anon_90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello everybody. I have a maze program built and it works to a point then it gets stuck. Any ideas?

    Thanks in advance.

    Here is the code:

    HTML:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    void mazeTraverse(char [][12], int, int);
    
    int main()
    {
    	char maze[12][12] =
    	{
    		'#','#','#','#','#','#','#','#','#','#','#','#',
    		'#','.','.','.','#','.','.','.','.','.','.','#',
    		'.','.','#','.','#','.','#','#','#','#','.','#',
    		'#','#','#','.','#','.','.','.','.','#','.','#',
    		'#','.','.','.','.','#','#','#','.','#','.','.',
    		'#','#','#','#','.','#','.','#','.','#','.','#',
    		'#','.','.','#','.','#','.','#','.','#','.','#',
    		'#','#','.','#','.','#','.','#','.','#','.','#',
    		'#','.','.','.','.','.','.','.','.','#','.','#',
    		'#','#','#','#','#','#','.','#','#','#','.','#',
    		'#','.','.','.','.','.','.','#','.','.','.','#',
    		'#','#','#','#','#','#','#','#','#','#','#','#'
    	};
    
    	mazeTraverse(maze,2,0);
    
    	return 0;
    }
    
    void mazeTraverse(char m[][12], int posY, int posX)
    {
    	static int update = time(0) % 10;
    
    	while(time(0) % 10 != update);
    	if(time(0) % 10 == 9)
    		update = 0;
    	else
    		update = (time(0) % 10) + 1;
    
    	m[posY][posX] = 'X';
    
    	system("cls");
    
    	//print maze
    	for(int row = 0; row < 12; row++)
    	{
    		for(int col = 0; col < 12; col++)
    		cout << m[row][col];
    
    		cout << '\n';
    	}
    
    	if(m[posY][posX] == 'E')
    	{
    		cout << "\n FOUND IT!! \n";
    		return;
    	}
    	else
    	{
    		if(m[posY-1][posX] != '#')
    		{
    			mazeTraverse(m, posY-1, posX);
    		}
    		else if(m[posY-1][posX] == '#' && m[posY][posX+1] != '#')
    		{
    			mazeTraverse(m, posY, posX+1);
    		}
    
    		if(m[posY][posX+1] != '#')
    		{
    			mazeTraverse(m, posY, posX+1);
    		}
    		else if(m[posY][posX+1] == '#' && m[posY+1][posX] != '#')
    		{
    			mazeTraverse(m, posY+1, posX);
    		}
    
    		if(m[posY+1][posX] != '#')
    		{
    			mazeTraverse(m, posY+1, posX);
    		}
    		else if(m[posY+1][posX] == '#' && m[posY][posX-1] != '#')
    		{
    			mazeTraverse(m, posY, posX-1);
    		}
    
    		if(m[posY][posX-1] != '#')
    		{
    			mazeTraverse(m, posY, posX-1);
    		}
    		else if(m[posY][posX-1] == '#' && m[posY-1][posX] != '#')
    		{
    			mazeTraverse(m, posY-1, posX);
    		}
    		
    
    		m[posY][posX] = '.';
    		return;
    	}
    }
    
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    There's no E in the grid. The E appears to be the exit condition, without which the program will search infinitely and use infinite memory.
     

Share This Page