d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Recursive Method In Ruby Path_finding?
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jun 4 2013 07:53am
Pretty much I'm given a maze from a file, I'm trying tor recursively call a squares so that I can find all the ways through the maze.

The file's form is:

Code
size_of_maze, start_x, start_y, end_x, end_y  #where each of these is a number, this is the header line of the file (first line)
0 0 ud                            #Followed by the coordinates that tell me how the maze is set up and what walls are open for movements.
0 1 udl                            #u means the top wall is open, d means the bottom wall is open, r means the right wall is open, l means the left wall is open
0 2 rd                             # I pretty much have to iterate through the maze finding every available path to the end of the maze from the start
....                                 # you can only move through open walls aka you have to be given the option to move R, L, U, or D


Kinda confused how to this, pretty new at ruby and not sure if/how recursion differs in ruby from C

Any help would be greatly appreciated

This post was edited by lopelurag on Jun 4 2013 07:54am
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Jun 5 2013 06:23am
Written in Python, not Ruby, but hopefully the logic might help you.
Probably could be done at lot better, but this is what first came to mind:

Code
def Find_Paths(list_of_paths, end_x, end_y):
   done = True
   new_paths=[]
   for path in list_of_paths:
       #for each input path, set start point as last square moved into
       start_x = path[-1][0]
       start_y = path[-1][1]
       for opendirection in maze[start_x, start_y]:
           #for each possible move from start point
           new_direction = direction[opendirection]
           new_square_x = start_x + new_direction[0]
           new_square_y = start_y + new_direction[1]
           new_square = [new_square_x,new_square_y]
           if new_square in path:
               #if we reach this point, the path has doubled back and will be abandoned
               pass
           elif (new_square_x == end_x) and (new_square_y == end_y):
               #if we reach this point, we have a solution, store it
               temp=[]
               for entry in path:
                   # rebuild the solution so it can be stored properly
                   temp.append(entry)
               temp.append(new_square)
               final_paths.append(temp)
           else:
               #if we reach this point this path is not finished yet, we will save it, so we can call function again
               done = False
               temp = []
               for entry in path:
                   #rebuild the candidate path so it can be used properly
                   temp.append(entry)
                   temp.append(new_square)
                   new_paths.append(temp)
   if done == False:
       #at this point there are still unfinished paths, so need to call function again to grind those out
       Find_Paths(new_paths, end_x, end_y)

 
 
#the following defines how a move changes the coordinates
direction = {'u':(0,1),'d':(0,-1), 'l':(-1,0), 'r': (1,0)}

#description of maze follows
maze = {}
maze[0,0] = 'ur'
maze[1,0] = 'lur'
maze[2,0] = 'ul'
maze[0,1] = 'udr'
maze[1,1] = 'lurd'
maze[2,1] = 'uld'
maze[0,2] = 'dr'
maze[1,2] = 'lrd'
maze[2,2] = 'ld'

starting_point = [0,0]
ending_point = [2,2]
start_x = starting_point[0]
start_y = starting_point[1]
end_x = ending_point[0]
end_y = ending_point[1]

#list to store solutions in
final_paths=[]

#list to store candidate paths in
list_of_paths = []
#i found it easier to append a fake starting point of -1,-1
list_of_paths.append([[-1,-1],[start_x,start_y]])

Find_Paths(list_of_paths, end_x, end_y)
for path in final_paths:
   #for each solution, pop off our fake -1,-1 starter
   path.pop(0)
   print path


This post was edited by Azrad on Jun 5 2013 06:35am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll