擅长:python、mysql、java
<p>与我之前的评论一致,这里有一个遍历多边形的简单迭代方法。它似乎给出了您指定的内容,而不使用全局变量甚至递归。你只需要这些吗?你知道吗</p>
<pre><code>def route_cal(start_node, move_list):
# For each step in the circuit
here = start_node
path = [here]
for step in range(len(move_list)):
# Find any legal move to an adjacent vertex
dest = move_list[here]
for here in dest:
if here not in path: # Don't repeat a vertex
path.append(here)
break
path.append(start_node)
# Now that we've found the full circuit, build a dictionary in each direction
path_len = len(path)
forward = {n: path[n] for n in range(path_len)}
reverse = {n: path[path_len-n-1] for n in range(path_len)}
return [forward, reverse]
legal_action = ((4,1), (0,2), (1,3), (2,4), (0,3))
print route_cal(0, legal_action)
</code></pre>