Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1.     visited = []
  2.     parent = {}
  3.     parent[problem.getStartState()] = -1
  4.     stack = util.Stack()
  5.     stack.push(problem.getStartState())
  6.     goal = problem.getStartState()
  7.  
  8.     while not stack.isEmpty():
  9.         vertex = stack.pop()
  10.         visited.append(vertex)
  11.        
  12.         if problem.isGoalState(vertex):
  13.             goal = vertex
  14.             break
  15.        
  16.         for obj in problem.getSuccessors(vertex):
  17.             if obj[0] not in visited:
  18.                 stack.push(obj[0])
  19.                 parent[obj[0]] = vertex
  20.  
  21.     from game import Directions
  22.     s = Directions.SOUTH
  23.     w = Directions.WEST
  24.     e = Directions.EAST
  25.     n = Directions.NORTH
  26.    
  27.     action_list = []
  28.     while parent[goal] != -1:
  29.         if parent[goal][0] < goal[0]:
  30.             action_list.insert(0, e)
  31.         elif parent[goal][0] > goal[0]:
  32.             action_list.insert(0, w)
  33.         elif parent[goal][1] < goal[1]:
  34.             action_list.insert(0, n)
  35.         elif parent[goal][1] > goal[1]:
  36.             action_list.insert(0, s)
  37.            
  38.         goal = parent[goal]
  39.  
  40.     return action_list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement