示例#1
0
def breadthFirstSearch(problem):
  """
  Search the shallowest nodes in the search tree first.
  [2nd Edition: p 73, 3rd Edition: p 82]
  """
  "*** YOUR CODE HERE ***"
  from game import Directions
  from util import Queue
  Explored = {}
  Frontier = Queue()
  
  node = 0
  current = problem.getStartState()
  #print "bfs first state", current
  succ = problem.getSuccessors(current)
  for k in succ:
    #print "initial push",k
    Frontier.push([k]);

  while not (Frontier.isEmpty()):
      current = Frontier.pop()
      node = current[-1][0]
      #print "curr path and node",current,node,"explored status",(node in Explored),"\n"
      # check if done
      if (problem.isGoalState(node)):
          break
      Explored[node] = 1;
      if (len(Explored)%100) == 0:
          print "ex",len(Explored)
      #print node
      #Explored[node[0]] = 1;
      succ = problem.getSuccessors(node)
      for k in succ:
          if not (k[0] in Explored):
              if not (Frontier.isPresent(current + [k])):
                Frontier.push(current + [k]);
 
  sol = []
  for k in current:
    sol += [k[1]]

  #print current
  #print "action sol", sol
  return  sol