使用Python解决迷宫

时间:2014-04-01 18:59:27

标签: python robot maze

我的目标是制作一个允许机器人解决迷宫的功能。对于课堂练习目的,这里的深度优先搜索是模板......

Todo = (root,[])
visited = []
while Todo not empty:
   node,path = Todo[0]
   Todo = Todo[1:]
   if node in visited:
       pass
   else:
       children = node.children
       children.remove(visited)
       if node is good:
           done(return path)
       Todo = [(child, path[child])]
           for child in children

机器人只能向前或向右转,我想知道在代码中命名每个模板的内容......例如,什么会儿童"是或"节点"?

我使用Calico(可让我在python中编程)和Myro库来执行此操作。

这是一个相当晚的帖子,但对于那些感兴趣的人最终成为最终的DFS。 还有计划行动的代码

class Node:
    def __init__(self, x, y, direction):
        self.x = x
        self.y = y
        self.direction = direction

    __left = { 'N' : 'W',
               'E' : 'N',
               'S' : 'E',
               'W' : 'S' }

    __right = { 'N' : 'E',
                'E' : 'S',
                'S' : 'W',
                'W' : 'N' }

    __dx = { 'N' : 0,
             'E' : 1,
             'S' : 0,
             'W' : -1 }

    __dy = { 'N' : 1,
             'E' : 0,
             'S' : -1,
             'W' : 0 }

def __str__(self):
    return "<%d,%d,%s>" % (self.x, self.y, self.direction)


def _left(self):
    return Node(self.x, self.y,
                self.__left[self.direction])

def _right(self):
    return Node(self.x, self.y,
                self.__right[self.direction])

def _forward(self):
    return  Node(self.x + self.__dx[self.direction], 
                 self.y + self.__dy[self.direction], 
                 self.direction)

def children(self):      
    return [ ('left',    self._left()), 
             ('right',   self._right()),
             ('forward', self._forward()),
           ]

def dfs(node, soughtValue, visitedNodes):
     if node.x == soughtValue.x and node.y == soughtValue.y and node.direction ==      soughtValue.direction:
      return []

 newVisited = visitedNodes[:]
 newVisited.append(node)
 for action, adjNode in node.children():
      if adjNode not in newVisited:
          plan = dfs(adjNode, soughtValue, newVisited)
          if plan is not None:
              p = [action]
              p.extend(plan)
              return p
 return None

感谢所有答案!!

2 个答案:

答案 0 :(得分:0)

“Children”和“node”指的是tree data structure.节点中的项目 - 树中的节点或项目。子项指的是您在树中查看的节点下面的项目。

你的“Todo”元组看起来像第一个项目是有问题的节点,第二个项目是一个孩子的数组。这可以是表示树的嵌套数据结构。 children数组下的每个项本身都可以是节点元组。

我不确定你在这里用“模板”指的是什么。现在你有一棵空树,你的遍历应该什么也不做,因为没有什么可做的。理想情况下,树的内容可能是迷宫中可用的不同路径。

答案 1 :(得分:0)

假设有一个结构,如

class Node(object):
    def __init__(self):
        self.children = set()

你的深度优先看起来像:

Todo = [(root, [])]
visited = set()
while Todo:
    node, path = Todo.pop()
    path.append(node)
    if node is good:
        return path
    visited.add(node)
    children = node.children.copy()
    Todo.extend([(child, path[:]) for child in children if child not in visited])

Todo包含元组列表。每个元组都是一个节点,也是到达那里的路径。

测试将是

good = Node()
a = Node()
b = Node()
b.children.add(good)
c = Node()
root = Node()
root.children.add(a)
root.children.add(b)
root.children.add(c)
相关问题