Skip to content

Instantly share code, notes, and snippets.

@timkg
Created October 27, 2015 08:04
Show Gist options
  • Save timkg/9545e5d8c1131de0782d to your computer and use it in GitHub Desktop.
Save timkg/9545e5d8c1131de0782d to your computer and use it in GitHub Desktop.
from game import Directions
n = Directions.NORTH
s = Directions.SOUTH
w = Directions.WEST
e = Directions.EAST
fringe = util.Stack()
closed = set()
def makeNode(state, prevActions=[], action=False):
actions = []
if action in ( n, s, w, e ):
actions = prevActions + [action]
return ( state, actions )
def search(fringe):
if fringe.isEmpty():
raise Exception, 'depthFirstSearch could not find solution'
node = fringe.pop()
if problem.isGoalState(node[0]):
return node[1]
if node[0] not in closed:
closed.add(node[0])
for triple in problem.getSuccessors(node[0]):
fringe.push(makeNode(triple[0], node[1], triple[1]))
return search(fringe)
fringe.push(makeNode(problem.getStartState()))
return search(fringe)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment