コード例 #1
0
ファイル: MP1-1.py プロジェクト: reneezhu/cs440
def a_star(graph, visited, wait_list):
  while True:
    try:
      node = wait_list.pop(0)
      visited.append(node)
      if node.v == '.':
        bfs_mark_path(node)
        return True
      elif node.v == '%':
        continue
      else:
        neighbor=graph_fun.get_neighbor(graph, node)
        for one in neighbor:
          if one not in visited:
            if one not in wait_list:
              one.save_parent(node)
              one.update_g(node.g+1)
              one.update_f()
              wait_list.append(one)
            else:
              if one.g<node.g+1:
                one.save_parent(node)
                one.update_g(node.g+1)
                one.update_f()
        wait_list.sort(key=operator.attrgetter('f'))
    except IndexError:
      print "Sorry! Cannot find a path to the end!"
      return False
コード例 #2
0
ファイル: MP1-1.py プロジェクト: reneezhu/cs440
def dfs(graph, visited, node):
  visited.append(node)
  if node.v=='%':
    return False
  neighbor = graph_fun.get_neighbor(graph, node)
  for one in neighbor:
    if one not in visited:
      if one.v=='.':
        return True
      else:
        if dfs(graph, visited, one):
          one.update_value('.')
          return True
  return False
コード例 #3
0
ファイル: MP1-1.py プロジェクト: reneezhu/cs440
def greedy_bfs(graph, visited, node):
  if node.v == '.':
    return True
  elif node.v == '%':
    return False
  else:
    visited.append(node)
    neighbor = graph_fun.get_neighbor(graph, node)
    neighbor.sort(key=operator.attrgetter('h'))
    for one in neighbor:
      if one not in visited:
        if greedy_bfs(graph, visited, one):
          node.update_value('.')
          return True
    return False
コード例 #4
0
ファイル: MP1-1.py プロジェクト: reneezhu/cs440
def bfs(graph, visited, queue):
  while True:
    try:
      node = queue.popleft()
      visited.append(node)
      if node.v=='.':
        bfs_mark_path(node)
        return True
      elif node.v=='%':
        continue
      else:
        neighbor = graph_fun.get_neighbor(graph, node)
        #add new nodes to the queue
        for one in neighbor:
          if one not in visited:
            if one not in queue:
              one.save_parent(node)
              queue.append(one)
    except IndexError:
      print "Sorry! Cannot find a path to the end!"
      return False