Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
13-liner: get the cool shoe shine solution in Clear category for Can You Pass? by przemyslaw.daniel
def can_pass(matrix, first, second):
x, y = first
rh, rw = range(len(matrix)), range(len(matrix[0]))
cells = {(i, j) for i in rh for j in rw
if matrix[i][j] == matrix[x][y]}
stack, visited = [first], set()
while stack:
x, y = stack.pop()
if (x, y) == second: return True
visited |= {(x, y)}
neighbors = {(x+1, y), (x-1, y), (x, y+1), (x, y-1)}
stack += list(neighbors & cells - visited)
return False
Dec. 4, 2017