Advertisement
runewalsh

e^x

Jun 1st, 2016
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. from collections import namedtuple
  2. Direction = namedtuple('Direction', 'dx, dy, letter')
  3. FourDirections = (
  4.     Direction(1, 0, 'S'),
  5.     Direction(-1, 0, 'N'),
  6.     Direction(0, 1, 'E'),
  7.     Direction(0, -1, 'W'))
  8. LetterToDirection = {d.letter: d for d in FourDirections}
  9.  
  10. def go(point, dir):
  11.     return (point[0] + dir.dx, point[1] + dir.dy)
  12.  
  13. def at(lab, point):
  14.     return lab[point[0]][point[1]]
  15.  
  16. def manhattan_distance(a, b):
  17.     return abs(a[0] - b[0]) + abs(a[1] - b[1])
  18.  
  19. def checkio(maze_map):
  20.     start, end = (1, 1), (10, 10)
  21.     done = []
  22.     path = ''
  23.     return graph(start, end, done, path, maze_map)
  24.  
  25. def graph(start, end, done, path, maze):
  26.     done.append(start)
  27.     if end in done:
  28.         print(path)
  29.         return True, path
  30.  
  31.     for point, dir in where_to_go(start, end, maze):
  32.         if point not in done:
  33.             ok, npath = graph(point, end, done, path + dir.letter, maze)
  34.             if ok:
  35.                 return ok, npath
  36.     done.pop()
  37.     return False, None
  38.  
  39. def where_to_go(start, end, maze):
  40.     all = ((go(start, dir), dir) for dir in FourDirections)
  41.     valid = ((point, dir) for point, dir in all if at(maze, point) != 1)
  42.     return sorted(valid, key = lambda pd: manhattan_distance(end, pd[0]))
  43.  
  44. if __name__ == '__main__':
  45.     def fail(msg):
  46.         print(msg)
  47.         return False
  48.  
  49.     #This code using only for self-checking and not necessary for auto-testing
  50.     def check_route(func, labyrinth):
  51.         ok, route = func([[0 if c == '.' else 1 for c in row] for row in labyrinth])
  52.         if not ok:
  53.             return fail("Path wasn't found")
  54.  
  55.         pos = (1, 1)
  56.         goal = (10, 10)
  57.         for d in route:
  58.             move = LetterToDirection.get(d, None)
  59.             if not move:
  60.                 return fail("Wrong symbol in route")
  61.             pos = go(pos, move)
  62.             if pos == goal:
  63.                 return True
  64.             if at(labyrinth, pos) == 1:
  65.                 return fail("Player in the pit")
  66.         return fail("Player did not reach exit")
  67.  
  68.     # These assert are using only for self-testing as examples.
  69.     assert check_route(checkio, [
  70.         "############",
  71.         "#..........#",
  72.         "#.######.###",
  73.         "#.#........#",
  74.         "#.#.######.#",
  75.         "#.#.#......#",
  76.         "#...##.###.#",
  77.         "#.#....#.###",
  78.         "#.##.#.....#",
  79.         "#.#..#####.#",
  80.         "#...##.....#",
  81.         "############"]), "First maze"
  82.     assert check_route(checkio, [
  83.         "############",
  84.         "#..........#",
  85.         "#..........#",
  86.         "#..........#",
  87.         "#..........#",
  88.         "#..........#",
  89.         "#..........#",
  90.         "#..........#",
  91.         "#..........#",
  92.         "#..........#",
  93.         "#..........#",
  94.         "############"]), "Empty maze"
  95.     assert check_route(checkio, [
  96.         "############",
  97.         "#.#...#....#",
  98.         "#.#.#.#.##.#",
  99.         "#.#.#.#.#..#",
  100.         "#.#.#.#.#.##",
  101.         "#.#.#.#.#..#",
  102.         "#.#.#.#.##.#",
  103.         "#.#.#.#.#..#",
  104.         "#.#.#.#.#.##",
  105.         "#.#.#.#.#..#",
  106.         "#...#...##.#",
  107.         "############"]), "Up and down maze"
  108.     assert check_route(checkio, [
  109.         "############",
  110.         "#...#...#..#",
  111.         "#.#...#...##",
  112.         "#...#...#..#",
  113.         "#.#...#...##",
  114.         "#...#...#..#",
  115.         "#.#...#...##",
  116.         "#...#...#..#",
  117.         "#.#...#...##",
  118.         "#...#...#..#",
  119.         "#.#...#....#",
  120.         "############"]), "Dotted maze"
  121.     assert check_route(checkio, [
  122.         "############",
  123.         "#....#.....#",
  124.         "#.##.#.###.#",
  125.         "#.##.#.#.#.#",
  126.         "#......###.#",
  127.         "######.#...#",
  128.         "#....###.###",
  129.         "#.##.......#",
  130.         "#.##.#####.#",
  131.         "#........###",
  132.         "#.#.#.##...#",
  133.         "############"]), "Need left maze"
  134.     assert check_route(checkio, [
  135.         "############",
  136.         "#..........#",
  137.         "###.####.###",
  138.         "#..........#",
  139.         "#.###.######",
  140.         "#..#.......#",
  141.         "##.######.##",
  142.         "#.....#....#",
  143.         "#####.#..#.#",
  144.         "#.....######",
  145.         "#.###......#",
  146.         "############"]), "The big dead end."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement