PDA

Click to See Complete Forum and Search --> : Algorithm to walk through maze...


iflash
Jan 16th, 2002, 10:37 AM
HI,

MY problem is to write a recursive function to walk through a maze. There is an algorithm. It's that you start at the right wall and walk forward and keep following the wall. You are guaranteed to find the exit and if there's none you'll come back to the starting point. But i really don't know where to start. The function should receive as arguments a 12-by-12 double-subscripted array representing the maze and the starting location. As it walks through the maze it replaces the path with the character 'X'. The function should display the maze after each move so the use can watch as the maze is solved. I need some help since i really don't know where to start. How to determine its path. Pls direct. Here is a sample maze:





############
# # #
# # #### #
### # # #
# ### #
#### # # # #
# # # # # #
## # # # # #
# # #
###### ### #
# # #
############



i know it looks a bit strange but i guess you'll figure it out. Thnx in advance!

CornedBee
Jan 17th, 2002, 08:31 AM
I don't think you'll get the function here (you need to write it yourself), but here are some tips:
The function needs to know where it is (surprise...). Best is having two variables that store the current indices in the array. Then the function must be able to find out what is right, in front, and left of it (maybe write a subroutine). Then it moves there and loops again, until it finds the exit. If you want to improve it, it should store the way it took and if it ever comes back to a field already visited, it deletes all steps in between. This is how you get the direct way through the maze.
And DON'T make the function recursive, it would require too many recursions. Make a loop.