1

I've been programming for about five years now and I had no trouble creating a dynamic maze. But now that it comes to recursive backtracking I have absolutely no idea where to start. I've read a lot of tutorials, topics and some algorhytm wiki's (dijkstra's algorhytm) and they make no sense to me.

I know how basic recursion works, but I simply can not begin to understand how recursive backtracking is even possible without (what it seems to me) storing previously searched paths or what happens if the path that is being tracked suddenly splits in two.

My program works like this: The maze consists out of 484 panels (Panel[] array named mazeTiles). There's 22 rows and each row holds 22 panels. Black panels backgrounds are walls, white ones are traversable.

This is what it looks like during runtime (and the error-message that should only display if there is no valid path from the starting square (red) to the green square in the upper left:

http://postimg.org/image/6c7wgxtz1/

The error message displayed is the one saying "Maze can not be solved" even though it can clearly be solved. This error message is located in the Button2_Click method.

And below is the code I've taken from a tutorial (and modified), the problem is definitely located within the method but I have no idea how to troubleshoot this.

    private void button2_Click(object sender, EventArgs e)
    {
        int startPosition = 0;

        for (int i = 0; i < mazeTiles.Length; i++)
        {
            if (mazeTiles[i].BackColor == Color.Red)
            {
                startPosition = i;
            }
        }

        bool[] alreadySearched = new bool[484];

        if (!solveMaze(startPosition, alreadySearched))
            MessageBox.Show("Maze can not be solved.");
    }

    private bool solveMaze(int position, bool[] alreadySearched)
    {
        bool correctPath = false;

        //should the computer check this tile
        bool shouldCheck = true;

        //Check for out of boundaries
        if (position >= mazeTiles.Length || position < 0 )
            shouldCheck = false;
        else
        {
            //Check if at finish, not (0,0 and colored light blue)
            if (mazeTiles[position].BackColor == Color.Green)
            {
                correctPath = true;
                shouldCheck = false;
            }
            //Check for a wall
            if (mazeTiles[position].BackColor == Color.Black)
                shouldCheck = false;
            //Check if previously searched
            if (alreadySearched[position])
                shouldCheck = false;
        }
        //Search the Tile
        if (shouldCheck)
        {
            //mark tile as searched
            alreadySearched[position] = true;
            //Check right tile
            correctPath = correctPath || solveMaze(position + 1, alreadySearched);
            //Check down tile
            correctPath = correctPath || solveMaze(position + 22, alreadySearched);
            //check left tile
            correctPath = correctPath || solveMaze(position - 1, alreadySearched);
            //check up tile
            correctPath = correctPath || solveMaze(position - 22, alreadySearched);
        }

        //make correct path gray
        if (correctPath)
            mazeTiles[position].BackColor = Color.Gray;

        return correctPath;
    }

I need to find and store all paths or the quickest one (and mark only the quickest one) from the red square to the green one. The green square is static but the red square and the entire maze are randomly generated.

user3776022
  • 217
  • 3
  • 12

2 Answers2

2

It's been two years and this post didn't get an answer. The answer that I was looking for back then was an explanation that made me realize how recursive backtracking worked. I had a lot of trouble figuring out how the method knew where it had been before, seemingly without storing visited areas in order to prevent going the same way over and over (infinite loop). But once I realized how it worked, I instantly figured out how to write the code as well.

So if anyone stumbles upon this while wondering the same thing. A simple way to understand recursive backtracking is to understand how recursive methods work. Recursive methods keep calling itself over and over until the right answer is found. Recursive backtracking keeps calling itself within itself in order to find the right answer. So a lot of basic recursive methods replace itelf so there's often only one instance of it at a time, while recursive backtracking methods often stack on top of each other.

When solving the puzzle the method keeps calling itself over and over within itself (inception-style) while constantly putting one step in a direction until there is no more steps in that direction. That's when that instance ends and the previous one that called it keeps going. And once that instance ends, the one before that will continue (and it can make a thousand inceptions of it's own).

If you're still having trouble understanding, think of how you're now trapped inside a maze. You have one superpower and it's not flying, it's cloning. So you keep moving forward until the path splits. This is when you clone yourself and send him on it's way in your place, while the original you does not move until the clone has found the exit or hit a dead end, that's when the clone teleports back to you and shares his information. But if your clone also runs into a split path, he clones himself and waits for his clone to return with knowledge of the maze. And that clone of your clone can also make an infinite number of clones based on how many times the path splits. Ultimately all that combined knowledge makes its way back to you. And that's when you'll know exactly where to find the exit of the maze.

user3776022
  • 217
  • 3
  • 12
0

Your problem is too broad. You need to post a specific question. It looks like you are trying to complete a homework assignment.

Your problem requires debugging, which would require someone to run your code. You might be better off just including a link to your code so someone can run it if they want to take the time to do that.

You would likely need a class level property to store the quickest path since you are already returning a bool, or you could use an out variable. The array you are passing to the recursive method would be where you store all the previously checked indexes I'm assuming.

KingOfHypocrites
  • 9,316
  • 9
  • 47
  • 69