Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import collections
  2. wall,clear,goal = 0, 1, 9
  3.  
  4. #PATH SHOULD BE [0, 0] , [0, 1] , [0, 2] , [1 , 2] 3 moves to get to 9
  5.  
  6. grid = ['100',
  7.         '100',
  8.         '191']
  9. width, height = 3, 3
  10.  
  11.  
  12. def bfs(grid, start):
  13.         queue = collections.deque([[start]])
  14.         seen = set([start])
  15.         while queue:
  16.                 path = queue.popleft()
  17.                 x, y = path[-1]
  18.                 if grid[y][x] == goal:
  19.                         return path
  20.                 for x2, y2 in ((x+1, y), (x-1, y), (x, y+1), (x, y-1)):
  21.                         if 0 <= x2 < width and 0 <= y2 < height and grid[y2][x2] != wall and (x2, y2) not in seen:
  22.                                 queue.append(path + [(x2, y2)])
  23.                                 seen.add((x2, y2))
  24.                                 print("in 2nd loop", path)
  25.  
  26.  
  27. path = bfs(grid, (0, 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement