Maze Solver with right-hand method

anon_90's Avatar, Join Date: Oct 2011
Newbie Member
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 Code:
#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;
	}
}
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.