Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Open Labyrinth by laharah
import itertools
def get_adj_point(y, x, dir):
""" returns a point adjecent to given cords in a given direction"""
MOVE = {"S": (1, 0), "N": (-1, 0), "W": (0, -1), "E": (0, 1)}
return (y + MOVE[dir][0], x + MOVE[dir][1])
def checkio(maze_map):
"""
This solution uses the basic "corn maze" solution:
"To get through any maze, place your hand on the left wall and follow it
all the way through."
Not the shortest route, but guaranteed to work on any maze, provided you
start on an "exterior" wall.
"""
moves = []
dirs = ['W', 'N', 'E', 'S']
current_dir = 1
left_dir = 0
x = y = 1
while y != 10 or x != 10:
l_y, l_x = get_adj_point(y, x, dirs[left_dir]) # block on "left"
f_y, f_x = get_adj_point(y, x, dirs[current_dir]) # block "in front"
if not maze_map[l_y][l_x]: # can move left, turn "left"
y = l_y
x = l_x
moves.append(dirs[left_dir])
current_dir = left_dir
left_dir = (left_dir - 1) % 4
elif maze_map[f_y][f_x]: # can't move forward, turn "right"
left_dir = current_dir
current_dir = (current_dir + 1) % 4
else: # can move forward
y = f_y
x = f_x
moves.append(dirs[current_dir])
return ''.join(moves)
April 8, 2015
Comments: