Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Can You Pass? by drpepper1412
def find_nb(cell):
up = (cell[0]-1, cell[1])
down = (cell[0]+1, cell[1])
left = (cell[0], cell[1]-1)
right = (cell[0], cell[1]+1)
fl = {up, down, left, right}
return fl
def can_pass(matrix, first, second):
value = matrix[first[0]][first[1]]
check = []
for i, j in enumerate(matrix):
for k, l in enumerate(j):
if l == value:
check.append((i, k))
visited, stack = set(), [first]
while stack:
item = stack.pop()
visited.add(item)
nb = find_nb(item)
for i in list(nb & set(check)):
if i not in visited:
if i == second:
return True
else:
stack.append(i)
return False
if __name__ == '__main__':
assert can_pass(((0, 0, 0, 0, 0, 0),
(0, 2, 2, 2, 3, 2),
(0, 2, 0, 0, 0, 2),
(0, 2, 0, 2, 0, 2),
(0, 2, 2, 2, 0, 2),
(0, 0, 0, 0, 0, 2),
(2, 2, 2, 2, 2, 2),),
(3, 2), (0, 5)) == True, 'First example'
assert can_pass(((0, 0, 0, 0, 0, 0),
(0, 2, 2, 2, 3, 2),
(0, 2, 0, 0, 0, 2),
(0, 2, 0, 2, 0, 2),
(0, 2, 2, 2, 0, 2),
(0, 0, 0, 0, 0, 2),
(2, 2, 2, 2, 2, 2),),
(3, 3), (6, 0)) == False, 'First example'
Aug. 11, 2018