Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
depth-first search solution in Clear category for Hypercube by David_Jones
def hypercube(grid):
def possible_to_read(i, j, rest_letters):
if not rest_letters:
return True
ch = rest_letters.pop()
for x,y in ((i-1,j), (i,j-1), (i,j+1), (i+1,j)):
if (0 <= x < n and 0 <= y < n and grid[x][y] == ch
and possible_to_read(x, y, rest_letters)):
return True
rest_letters.append(ch)
return False
n = len(grid)
for i in range(n):
for j in range(n):
grid[i][j] = grid[i][j].lower()
rest_letters = list('ebucrepy')
for i in range(n):
for j in range(n):
if grid[i][j] == 'h' and possible_to_read(i, j, rest_letters):
return True
return False
May 10, 2019