runewalsh

bfs

Jun 1st, 2016
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. from collections import namedtuple, deque
  2. Direction = namedtuple('Direction', 'dx, dy, letter')
  3. FourDirections = [Direction(dx, dy, letter)
  4.                   for dx, dy, letter in ((1, 0, 'S'), (-1, 0, 'N'), (0, 1, 'E'), (0, -1, 'W'))]
  5. LetterToDirection = {d.letter: d for d in FourDirections}
  6.  
  7. def go(point, dir):
  8.     return (point[0] + dir.dx, point[1] + dir.dy)
  9.  
  10. def at(lab, point):
  11.     return lab[point[0]][point[1]]
  12.  
  13. ChainedCell = namedtuple('ChainedCell', 'cell, dir')
  14. def unwind(came_from, last):
  15.     path, cur = "", came_from.get(last)
  16.     while cur:
  17.         path = cur.dir.letter + path
  18.         cur = came_from.get(cur.cell)
  19.     print(path)
  20.     return path
  21.  
  22. def find_way(maze, start, end):
  23.     came_from = {start: False}
  24.     next = deque([start])
  25.  
  26.     while len(next) > 0:
  27.         cell = next.popleft()
  28.         if cell == end:
  29.             return True, unwind(came_from, cell)
  30.  
  31.         for dir in FourDirections:
  32.             ncell = go(cell, dir)
  33.             if at(maze, ncell) == '.' and came_from.get(ncell) is None:
  34.                 next.append(ncell)
  35.                 came_from[ncell] = ChainedCell(cell, dir)
  36.     return False, None
  37.  
  38. if __name__ == '__main__':
  39.     def waypts(start, way):
  40.         r = set([start])
  41.         cur = start
  42.         for dsym in way:
  43.             cur = go(cur, LetterToDirection[dsym])
  44.             r.add(cur)
  45.         return r
  46.  
  47.     maze = [
  48.         "############",
  49.         "#..........#",
  50.         "###.####.###",
  51.         "#..........#",
  52.         "#.###.######",
  53.         "#..#.......#",
  54.         "##.######.##",
  55.         "#.....#....#",
  56.         "#####.#..#.#",
  57.         "#.....######",
  58.         "#.###......#",
  59.         "############"]
  60.     start, end = (1, 1), (10, 10)
  61.     ok, way = find_way(maze, start, end)
  62.     if ok:
  63.         pts = waypts(start, way)
  64.         print('\n'.join(''.join('w' if (y, x) in pts else c for x, c in enumerate(row)) for y, row in enumerate(maze)))
  65.     else:
  66.         print("Cannot find the way.")
Add Comment
Please, Sign In to add comment