Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Can You Pass? by vvm70
def can_pass(matrix, first, second):
matrix = [list(row) for row in matrix]
num = matrix[first[0]][first[1]]
mark = (num + 1) % 10
stack = [first]
while stack:
row, col = stack.pop()
if row > 0 and matrix[row-1][col] == num:
stack.append((row-1, col))
if row < len(matrix)-1 and matrix[row+1][col] == num:
stack.append((row+1, col))
if col > 0 and matrix[row][col-1] == num:
stack.append((row, col-1))
if col < len(matrix[0])-1 and matrix[row][col+1] == num:
stack.append((row, col+1))
matrix[row][col] = mark
if (row, col) == second:
return True
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'
Feb. 27, 2021
Comments: