search.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:Berkeley-AI-PacMan-Lab-1 作者: jrios6 项目源码 文件源码
def uniformCostSearch(problem):
  "Search the node of least total cost first. "
  frontier = PriorityQueue()
  startNode = Node((problem.getStartState(), None, None))

  #Check if start node is goal
  if problem.isGoalState(startNode.state):
      return []

  for successors in problem.getSuccessors(problem.getStartState()):
      newNode = Node(successors, startNode)
      frontier.push(newNode, 0)

  explored = list()
  explored.append(startNode.state)

  while not frontier.isEmpty():
      leafNode = frontier.pop()
      if problem.isGoalState(leafNode.state):
          return leafNode.getPath()
      explored.append(leafNode.state)
      for successor in problem.getSuccessors(leafNode.state):
          newNode = Node(successor, leafNode)
          if newNode.state not in frontier.stateList and newNode.state not in explored:
              frontier.push(newNode, newNode.pathCost)
  return []
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号