Hullo. My latest assignment in computar class has me trying to create a maze solver. Now it is not necessary for the computer to solve for the shortest path, however there are delicious extra marks if it does. I've already figured out how to solve the maze - using the recursive backtracking technique. I know a breadth search is used to find the shortest path in a maze solving problem. However I am having great trouble in trying to implement this algorithm (recursively).

Now, this isn't done in the C programming language, its suppose to be done in Java. But the syntax is so similar you can post C code and I'll be able to tell what it is.

Now you start at S and place that node into an array specifically meant to hold nodes. Then you look up, down, left, and right for any 'path' symbols. If there is path symbol, place it in a que array. Then recursively call the method turning the recently discovered que into the new node. Continue until exit is found. I've tried doing this but it doesn't work. It will find the exit, but it is not the shortest path. Is there something I'm missing from the algorithm? I'm having some trouble understanding the algorithm, this is what I've got out of it so far.

04-05-2009

Adak

Do you have to use breadth first search?

In my (limited) experience, it's a pita, and noticeably slower than using depth first search, despite what you read in theory books.

I'm confident we can find the shortest path with it.

I'll post up a little example of it, in an edit to this, and see if you're not *really* tempted by it. :)

Yes a breadth-first search is essentially going to find the shortest path, but it will be very slow!
To speed it up, rather than examining all paths of length n before those of length n+1, you have a heuristic that biases it towards following those paths that are getting you measurably closer to the goal. This is the basic idea behind the algorithm called A*. That algorithm is what you need to read up on.

04-07-2009

Raskalnikov

I got it to work :)

Turns out there was a lot more involved then I thought there was. We didn't have to use breadth first search I just decided on that one because it seemed to be the easiest out of the 'find the quickest route' bunch. Thanks for the suggestions and thanks Adak for the code on depth, I'll sure use it for reference.

04-07-2009

Adak

Well done, R Man!

04-07-2009

quzah

BFS is a really fun exercise. You could have really wowed your professor if you had let them pick which method they wanted to use from a menu. ;)

Anyway, back to BFS:

Code:

add the starting point to the first list
while not at the exit
for each room in one list
if not exit
add its neighbors to the other list
else
return the shortest path
swap lists