不好意思怕有人误会
得先说明这是作业,但没要求解释以下的问题
最近在写berkelyey pacman
有3个步骤有点不太明白
想请问
(1)
node, actions, visited = fringe.pop()
一次把右边的pop assign给左边的多个variables是什么意思呢?
(2)
fringe.push((coord, actions+[direction], visited+[node]))
push定义如下,我的理解是依序把coord, actions+[direction], visited+[node]
加入到list中,不知是否正确呢?
(3)
for coord, direction, steps in problem.getSuccessors(node):
我的程度只到 for i in list:
有点不太明白for 后面接多个variables跑的意思是什么
#Code:
def depthFirstSearch(problem)
fringe = util.Stack()
fringe.push( (problem.getStartState(), [], []) )
while not fringe.isEmpty():
node, actions, visited = fringe.pop()
for coord, direction, steps in problem.getSuccessors(node):
if not coord in visited:
if problem.isGoalState(coord):
return actions + [direction]
fringe.push((coord, actions + [direction], visited + [node] ))
return []
#util
class Stack:
"A container with a last-in-first-out (LIFO) queuing policy."
def __init__(self):
self.list = []
def push(self,item):
"Push 'item' onto the stack"
self.list.append(item)
def pop(self):
"Pop the most recently pushed item from the stack"
return self.list.pop()
def isEmpty(self):
"Returns true if the stack is empty"
return len(self.list) == 0