回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>当我执行它时,它给了我一个eror,即太多的值,无法解包?
我怎样才能使它正常工作。</p>
<pre><code> stack = util.Stack()
closed = []
child = []
index = 0
currNode = problem.getStartState()
node = currNode
stack.push(node)
while not stack.isEmpty():
node = stack.pop()
if problem.isGoalState(node):
print "true"
closed.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(node)
else:
child = problem.getSuccessors(node)
for nodes in child:
stack.push(nodes)
closed.append(node)
return None
</code></pre>
<p>错误是:</p>
<pre><code> File line 90, in depthFirstSearch
child = problem.getSuccessors(node)
File line 179, in getSuccessors
x,y = state
**ValueError: too many values to unpack**
</code></pre>
<p>getsuccession函数的代码是:</p>
<pre><code>def getSuccessors(self, state):
"""
Returns successor states, the actions they require, and a cost of 1.
"""
successors = []
for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:
x,y = state
dx, dy = Actions.directionToVector(action)
nextx, nexty = int(x + dx), int(y + dy)
if not self.walls[nextx][nexty]:
nextState = (nextx, nexty)
cost = self.costFn(nextState)
successors.append( ( nextState, action, cost) )
</code></pre>
<p>最初为此函数返回的值:</p>
<pre><code>problem.getStartState() - (5, 5)
problem.isGoalState(problem.getStartState())- False
problem.getSuccessors(problem.getStartState()) - [((5, 4), 'South', 1), ((4, 5), 'West', 1)]
</code></pre>