Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in 3rd party category for Can You Pass? by aya.kanazawa
import numpy as np
def can_pass(matrix, first, second):
grid = np.array(matrix)
start_num = grid[first[0]][first[1]]
rows, columns = len(grid), len(grid[0])
grid = np.where(grid != start_num, -9, grid) # -9 is NG
grid[first[0]][first[1]] = -1 # -1 is OK
cells_to_check = [first]
while cells_to_check:
r, c = cells_to_check.pop()
# upper, lower, left, right
for nr, nc in [(r-1, c),(r+1, c),(r, c-1),(r, c+1)]:
if 0 <= nr <= rows -1 and 0 <= nc <= columns - 1 \
and grid[nr][nc] >= 0:
grid[nr][nc] = -1
cells_to_check.append((nr,nc))
print(f'grid={grid} cells_to_check={cells_to_check}')
if grid[second[0]][second[1]] < 0:
break
return grid[second[0]][second[1]] == -1
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, 'Second example'
June 11, 2019
Comments: