Skip to content

Instantly share code, notes, and snippets.

@5hirish
Created July 27, 2016 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5hirish/4cd1eb609223e5cc930ae80f38e699db to your computer and use it in GitHub Desktop.
Save 5hirish/4cd1eb609223e5cc930ae80f38e699db to your computer and use it in GitHub Desktop.
def solveMaze (Maze , position , N ,destination):
# returns a list of the paths taken
if position == destination:
return [ destination ]
x , y = position
if x + 1 < N and Maze[x+1][y] == 1: # Down
a = solveMaze( Maze , ( x + 1 , y ) , N , destination)
print("At",a)
if a != None:
return [ (x , y ) ] + a
if y + 1 < N and Maze[x][y+1] == 1: # Left
b = solveMaze( Maze , (x , y + 1) , N , destination)
print("At",b)
if b != None:
return [ ( x , y ) ] + b
if y - 1 < N and Maze[x][y-1] == 1: # Right
c = solveMaze( Maze , (x , y - 1) , N , destination)
print("At",c)
if c != None:
return [ ( x , y ) ] + c
if x - 1 < N and Maze[x][y-1] == 1: # Right
d = solveMaze( Maze , (x - 1, y ) , N , destination)
print("At",d)
if d != None:
return [ ( x , y ) ] + d
"""Maze = [[ 1 , 0 , 1, 0 , 0],
[ 1 , 1 , 0, 1 , 0],
[ 0 , 1 , 0, 1 , 0],
[ 0 , 1 , 0, 0 , 0],
[ 1 , 1 , 1, 1 , 1]
]"""
Maze = [[ 0, 1, 1, 0],
[ 0, 1, 0, 1],
[ 0, 1, 1, 1],
[ 1, 1, 0, 1]]
print (solveMaze(Maze,(3,3),4,(0,2)))
#print (solveMaze(Maze,(0,0),5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment