Results 1 to 2 of 2

Thread: Algorithm to walk through maze...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484

    Algorithm to walk through maze...

    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:



    Code:
    ############
    #   #      #
      # # #### #
    ### #    # #
    #    ### #  
    #### # # # #
    #  # # # # #
    ## # # # # #
    #        # #
    ###### ### #
    #      #   #
    ############

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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width