Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- result = ''
-
-
- def checkio(maze_map):
- global result
- result = ''
- start, end = [1, 1], [10, 10]
- done = []
- path = ''
- graph(start, end, done, path, maze_map)
- return result
-
-
- def graph(start, end, done, path, maze):
- global result
- done.append(start)
- if end in done:
- result = ''.join([x for x in path])
- print(result)
- if result == '':
- where_to_go = where_to(start, end, maze)
- for step in where_to_go:
- if step not in done:
- pathdone = [len(path), len(done)]
- path += wnes(start, step)
- start = step
- graph(start, end, done, path, maze)
- path, done = path[:pathdone[0]], done[:pathdone[1]]
-
-
- def wnes(start, step):
- coords = {'S': lambda x, y: x[0] < y[0],
- 'N': lambda x, y: x[0] > y[0],
- 'W': lambda x, y: x[1] > y[1],
- 'E': lambda x, y: x[1] < y[1],}
- for elem in coords:
- if coords.get(elem)(start, step) is True:
- return elem
-
-
- def where_to(start, end, maze):
- return sorted([x for x in [[start[0] + 1, start[1]],
- [start[0] - 1, start[1]],
- [start[0], start[1] + 1],
- [start[0], start[1] - 1]] if maze[x[0]][x[1]] != 1],
- key=lambda x: (end[0] + end[1]) - (x[0] + x[1]))
-
- if __name__ == '__main__':
- #This code using only for self-checking and not necessary for auto-testing
- def check_route(func, labyrinth):
- MOVE = {"S": (1, 0), "N": (-1, 0), "W": (0, -1), "E": (0, 1)}
- #copy maze
- route = func([row[:] for row in labyrinth])
- pos = (1, 1)
- goal = (10, 10)
- for i, d in enumerate(route):
- move = MOVE.get(d, None)
- if not move:
- print("Wrong symbol in route")
- return False
- pos = pos[0] + move[0], pos[1] + move[1]
- if pos == goal:
- return True
- if labyrinth[pos[0]][pos[1]] == 1:
- print("Player in the pit")
- return False
- print("Player did not reach exit")
- return False
-
- # These assert are using only for self-testing as examples.
- assert check_route(checkio, [
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
- [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 1],
- [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
- [1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
- [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
- [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1],
- [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
- [1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]), "Up and down maze"
- assert check_route(checkio, [
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1],
- [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1],
- [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1],
- [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1],
- [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1],
- [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1],
- [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1],
- [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1],
- [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1],
- [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]), "Dotted maze"
- assert check_route(checkio, [
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
- [1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1],
- [1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1],
- [1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1],
- [1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1],
- [1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1],
- [1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
- [1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]), "Need left maze"
- assert check_route(checkio, [
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1],
- [1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1],
- [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1],
- [1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
- [1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]), "The big dead end."
- print("The local tests are done.")
Advertisement
Add Comment
Please, Sign In to add comment