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 yuwai
def can_pass(matrix, first, second):
coordinate=[]
for x in range(len(matrix)):
for y in range(len(matrix[0])):
if matrix[x][y]==matrix[first[0]][first[1]]:
coordinate.append((x,y))
path=set([first])
temp=set([first])
while temp:
temp=set()
for x,y in path:
for dx,dy in ((1,0),(0,1),(-1,0),(0,-1)):
nx,ny=x+dx,y+dy
if (nx,ny) in coordinate :
coordinate.remove((nx,ny))
temp.add((nx,ny))
path|=temp
return (second[0],second[1]) in path
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'
June 25, 2018