Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
refactored recursive descent solution in Clear category for Can You Pass? by juestr
def can_pass(matrix, first, second):
sizey, sizex = len(matrix), len(matrix[0])
def moves(at):
return ((at[0] + y, at[1] + x) for y, x in ((0, 1), (1, 0), (0, -1), (-1, 0)))
def isvalid(at):
return 0 <= at[0] < sizey and 0 <= at[1] < sizex
def cell(at):
return matrix[at[0]][at[1]]
def search(at, path=[]):
return at == second or \
any(search(to, path + [to]) for to in moves(at)
if isvalid(to) and cell(at) == cell(to) and to not in path)
return search(first)
Nov. 3, 2019
Comments: