Guest User

Untitled

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