def depthFirstSearch(problem):
"""
Search the deepest nodes in the search tree first.
Your search algorithm needs to return a list of actions that reaches the
goal. Make sure to implement a graph search algorithm.
To get started, you might want to try some of these simple commands to
understand the search problem that is being passed in:
print "Start:", problem.getStartState()
print "Is the start a goal?", problem.isGoalState(problem.getStartState())
print "Start's successors:", problem.getSuccessors(problem.getStartState())
"""
"*** YOUR CODE HERE ***"
startState = problem.getStartState()
visited = set()
fringe = util.Stack()
fringe.push((startState, ()))
while not fringe.isEmpty():
currNode = fringe.pop()
currState = currNode[0]
currPlan = currNode[1]
if problem.isGoalState(currState):
return list(currPlan)
if not currState in visited:
visited.add(currState)
paths = problem.getSuccessors(currState)
for path in paths:
newPlan = list(currPlan)
newPlan.append(path[1])
nextNode = (path[0], tuple(newPlan))
if not path[0] in visited:
fringe.push(nextNode)
评论列表
文章目录