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