Hey guys,
I have written a recursive function mazeTraverse which finds the path of the mouse to find its food which is placed inside the maze. I am given a maze of of 15 rows and 25 columns. Mouse's starting position is (8,1)[eighth row and 1st column] and the food is placed in (5, 10).
In my maze, 'A' represents the way is blocked
' ' represnts an opening
'M' represents starting position for the mouse and 'T' represents the target which is food.
I am supposed to print X which will start moving from starting position to the target. X basically represents the mouse movement from the starting position to the target. I know I need to use recursive function so when mouse find dead end it has to do backtracking. I have written a function but it is not correct. Can anyone look at my code and guide me. I am very interested to learn something new.

Here is my code :

class MazeMania
{
public:
  MazeMania(const int maze[][], int start, int end);
  int traverse( int x, int y);
private:
  const int ROWS = 15;
  const int COLS = 25;
  char mouse = 'X';
};

# include "mazeMania.h"

#define FALSE 0
#define TRUE 1


// initializes the constructor
MazeMania::MazeMania(const int maze[][], int start, int end)
{
  int start = 8;
  int end = 1;
}

int MazeMania::mazeTraverse(int x, int y)
{
  if ( x < 0 || x > COLS -1 || y < 0 || y > ROWS -1)
  {
    return FALSE;
  }

  if (maze[y][x] == 'A')
  {
    return FALSE;
  }
  cout <<" "<<mouse;
  // if x, y is the goal, return true
  if (maze[y][x] == 'T')
  {
    return TRUE;
  }

  if (mazeTraserse(x-1, y) == TRUE)
  {
    return TRUE;
  }
  else if (mazeTraverse (x, y-1) == TRUE)
  {
    return TRUE;
  }
  else if (mazeTraverse (x + 1, y) == TRUE)
  {
    return TRUE;
  }
  else if (mazeTraverse (x, y + 1) == TRUE)
  {
    return TRUE;
  }

I am confused now. Is my algorithm correct? How can I print 'X' now? and how can I display like X is moving from starting position to find the target? Please guide me through. I am willing to learn. Thanks

Recommended Answers

All 2 Replies

There's no ending bracket to your function, so is there more to the function? I see no change in the maze[][] array and no other variables that keep track of where you've been. You have cout statements that display an 'X', but what's the point of this? I get a return value of true or false, but assuming the maze is solvable, I'd better get true, right? But I get "true" returned, I still have no idea what path I took. As far as "backtracking" goes, there's no direction, there's just a coordinate or something, so how do I know how to reverse directions?

So if the goal is to decide WHETHER a maze is solvable, it might possibly work. If the goal is to determine the correct path, I don't see anything that has anything to do with a path here. I also think that possibly, you're going to have a massive call stack, though I'm not 100% positive. I don't understand the algorithm and I don't understand the purpose of the function.

What does your maze look like? You just give the start and finish points.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.