Guest User

Untitled

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