Skip to content

Instantly share code, notes, and snippets.

@abdellani
Created October 15, 2019 14:59
Show Gist options
  • Save abdellani/7372c16bbd9b8c2b1157c75c2075d4d8 to your computer and use it in GitHub Desktop.
Save abdellani/7372c16bbd9b8c2b1157c75c2075d4d8 to your computer and use it in GitHub Desktop.
def getNext(x,y,maze)
rows=maze.length
cols=maze.first.length
next_node=[]
next_node<<[x+1,y]if x<cols-1 && maze[x+1][y]!=1
next_node<<[x,y+1]if y<rows-1 && maze[x][y+1]!=1
next_node
end
def search_path(x,y,maze,path)
return path if maze[x][y]==9
next_nodes=getNext(x,y,maze)
next_nodes.each do |coors|
path<< [coors[1],coors[0]]
res=search_path(coors[0],coors[1],maze,path)
return path unless res.nil?
path.pop
end
nil
end
def maze_escape(maze)
# write your code here
search_path(0,0,maze,[[0,0]])
end
p maze_escape(
[
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 1, 0, 1, 1],
[0, 1, 0, 0, 0],
[0, 0, 0, 1, 9]
]
)
# => [
# [0, 0],
# [1, 0],
# [2, 0],
# [2, 1],
# [2, 2],
# [2, 3],
# [3, 3],
# [4, 3],
# [4, 4]
# ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment