Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import namedtuple
- Direction = namedtuple('Direction', 'dx, dy, letter')
- FourDirections = (
- Direction(1, 0, 'S'),
- Direction(-1, 0, 'N'),
- Direction(0, 1, 'E'),
- Direction(0, -1, 'W'))
- LetterToDirection = {d.letter: d for d in FourDirections}
- def go(point, dir):
- return (point[0] + dir.dx, point[1] + dir.dy)
- def at(lab, point):
- return lab[point[0]][point[1]]
- def manhattan_distance(a, b):
- return abs(a[0] - b[0]) + abs(a[1] - b[1])
- def checkio(maze_map):
- start, end = (1, 1), (10, 10)
- done = []
- path = ''
- return graph(start, end, done, path, maze_map)
- def graph(start, end, done, path, maze):
- done.append(start)
- if end in done:
- print(path)
- return True, path
- for point, dir in where_to_go(start, end, maze):
- if point not in done:
- ok, npath = graph(point, end, done, path + dir.letter, maze)
- if ok:
- return ok, npath
- done.pop()
- return False, None
- def where_to_go(start, end, maze):
- all = ((go(start, dir), dir) for dir in FourDirections)
- valid = ((point, dir) for point, dir in all if at(maze, point) != 1)
- return sorted(valid, key = lambda pd: manhattan_distance(end, pd[0]))
- if __name__ == '__main__':
- def fail(msg):
- print(msg)
- return False
- #This code using only for self-checking and not necessary for auto-testing
- def check_route(func, labyrinth):
- ok, route = func([[0 if c == '.' else 1 for c in row] for row in labyrinth])
- if not ok:
- return fail("Path wasn't found")
- pos = (1, 1)
- goal = (10, 10)
- for d in route:
- move = LetterToDirection.get(d, None)
- if not move:
- return fail("Wrong symbol in route")
- pos = go(pos, move)
- if pos == goal:
- return True
- if at(labyrinth, pos) == 1:
- return fail("Player in the pit")
- return fail("Player did not reach exit")
- # These assert are using only for self-testing as examples.
- assert check_route(checkio, [
- "############",
- "#..........#",
- "#.######.###",
- "#.#........#",
- "#.#.######.#",
- "#.#.#......#",
- "#...##.###.#",
- "#.#....#.###",
- "#.##.#.....#",
- "#.#..#####.#",
- "#...##.....#",
- "############"]), "First maze"
- assert check_route(checkio, [
- "############",
- "#..........#",
- "#..........#",
- "#..........#",
- "#..........#",
- "#..........#",
- "#..........#",
- "#..........#",
- "#..........#",
- "#..........#",
- "#..........#",
- "############"]), "Empty maze"
- assert check_route(checkio, [
- "############",
- "#.#...#....#",
- "#.#.#.#.##.#",
- "#.#.#.#.#..#",
- "#.#.#.#.#.##",
- "#.#.#.#.#..#",
- "#.#.#.#.##.#",
- "#.#.#.#.#..#",
- "#.#.#.#.#.##",
- "#.#.#.#.#..#",
- "#...#...##.#",
- "############"]), "Up and down maze"
- assert check_route(checkio, [
- "############",
- "#...#...#..#",
- "#.#...#...##",
- "#...#...#..#",
- "#.#...#...##",
- "#...#...#..#",
- "#.#...#...##",
- "#...#...#..#",
- "#.#...#...##",
- "#...#...#..#",
- "#.#...#....#",
- "############"]), "Dotted maze"
- assert check_route(checkio, [
- "############",
- "#....#.....#",
- "#.##.#.###.#",
- "#.##.#.#.#.#",
- "#......###.#",
- "######.#...#",
- "#....###.###",
- "#.##.......#",
- "#.##.#####.#",
- "#........###",
- "#.#.#.##...#",
- "############"]), "Need left maze"
- assert check_route(checkio, [
- "############",
- "#..........#",
- "###.####.###",
- "#..........#",
- "#.###.######",
- "#..#.......#",
- "##.######.##",
- "#.....#....#",
- "#####.#..#.#",
- "#.....######",
- "#.###......#",
- "############"]), "The big dead end."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement