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.