Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Brute Forcing it solution in Clear category for Open Labyrinth by stclair.daniel
import random
def possible_moves(row, col, matrix):
"""
From any point, determine which of the four directions are legal moves
Return a list of tuples with the move coordinates and string represensing
the direction (NSEW)
"""
possible_moves = list()
#NORTH
if row - 1 >= 0 and matrix[row-1][col] == 0:
possible_moves.append((row - 1, col, "N"))
#SOUTH
if row + 1 < len(matrix) and matrix[row+1][col] == 0:
possible_moves.append((row + 1, col, "S"))
#WEST
if col - 1 >= 0 and matrix[row][col-1] == 0:
possible_moves.append((row, col - 1, "W"))
#EAST
if col + 1 < len(matrix) and matrix[row][col+1] == 0:
possible_moves.append((row, col + 1, "E"))
return possible_moves
def checkio(matrix):
"""
Brute force method. From the initial point, choose a random legal move. Move,
add the direction to the path, repeat. If the move is the finish,
return the path. Could potentially get stuck for a while but should
eventually finish
"""
row = 1
col = 1
route = ""
while True:
moves = possible_moves(row,col,matrix)
move = random.choice(moves)
row = move[0]
col = move[1]
route += move[2]
if move[0] == 10 and move[1] == 10:
return route
March 27, 2015