大家好!我想用Python做一个算法,用DFS遍历一个映射。问题是,它会在访问一些好的部分后阻塞,并且不会遍历所有地图。我正在使用pygame,但我将仅共享我的dfs方法:
ef moveDSF(self, detectedMap):
if len(self.stack):
current = self.stack.pop()
if current not in self.visited:
self.visited.append(current)
self.x = current[0]
self.y = current[1]
# print(current, self.stack)
for position in v: # v = [[-1, 0], [1, 0], [0, 1], [0, -1]]
new_x = self.x + position[0]
new_y = self.y + position[1]
if 0 < new_x < 19 and 0 < new_y < 19 and detectedMap.surface[new_x][new_y] == 0:
neighbour = (new_x, new_y)
self.stack.append(neighbour)
return True
else:
return False
地图表示为20x20矩阵。在开始时,在堆栈中,我们将只有初始值,并且访问的将是一个空列表。我们在这个算法中一步一个脚印,因为while循环将在pygame部分。“detectedMap.surface[new_x][new_y]==0”检查该点是否正确,1表示墙
目前没有回答
相关问题 更多 >
编程相关推荐