Created
July 27, 2016 07:42
-
-
Save 5hirish/4cd1eb609223e5cc930ae80f38e699db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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