dvdjaco

CS373 - u4-8

Mar 14th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. def search():
  2.     open_nodes = []
  3.     open_nodes.append([0] + init)
  4.     seen = []
  5.     world_w = len(grid)
  6.     world_h = len(grid[0])
  7.  
  8.     # the loop ends if open empty (no more nodes to expand)
  9.     while open_nodes:
  10.         print open_nodes
  11.         # 1st item in the queue has the lowest g
  12.         current = open_nodes.pop(0)
  13.         pos = current[1:]
  14.         if pos == goal:
  15.             return current
  16.         g = current[0]
  17.         if pos not in seen:
  18.             seen.append(pos)
  19.                        
  20.         for movement in delta:
  21.             newcell = [pos[0] + movement[0], pos[1] + movement[1]]
  22.             newx = newcell[0]
  23.             newy = newcell[1]
  24.        
  25.             if (newcell not in seen and
  26.                 newcell[0] >= 0 and
  27.                 newcell[0] < world_w and
  28.                 newcell[1] >= 0 and
  29.                 newcell[1] < world_h and
  30.                 not grid[newx][newy]):
  31.                 open_nodes.append([g + cost] + newcell)
  32.                 seen.append(newcell)
  33.     return 'fail'
Advertisement
Add Comment
Please, Sign In to add comment