Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
BFS solution in Clear category for Can You Pass? by fed.kz
from collections import deque
def can_pass(matrix, start, finish):
def check(x, y, i, j):
return ((abs(i) + abs(j) == 1 and (x+i, y+j) not in visited and 0 <= x+i < len(matrix) and
0 <= y+j < len(matrix[0]) and matrix[x+i][y+j] == value))
value = matrix[start[0]][start[1]]
visited = {start}
d = deque([start])
while d:
x, y = d.popleft()
if (x, y) == finish: return True
near = {(x+i, y+j) for i, j in ((1,0), (0,1), (-1,0), (0, -1)) if check(x,y,i,j)}
visited |= near
d.extend(near)
return False
Sept. 26, 2018
Comments: