Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Hypercube by tigerhu3180
from heapq import heapify,heappush,heappop
STRING = 'hypercube'
def hypercube(grid):
h, v = len(grid), len(grid[0])
mp = [[(i, j, 0) for i in range(h) for j in range(v) if grid[i][j] in ('H','h')]] # x,y,current step
while mp:
route = heappop(mp)
x, y, step = route[-1]
if step == len(STRING) - 1:
return True
for m, n in ((-1, 0), (1, 0), (0, 1), (0, -1)):
nx ,ny = x + m, y + n
if 0 <= nx < h and 0 <= ny < v and grid[nx][ny].lower() == STRING[step + 1]:
route.append((nx, ny, step + 1))
heappush(mp, route)
return False
if __name__ == '__main__':
print("Example:")
print(hypercube([
['g', 'f', 'H', 'Y', 'v'],
['z', 'e', 'a', 'P', 'u'],
['s', 'B', 'T', 'e', 'y'],
['k', 'u', 'c', 'R', 't'],
['l', 'O', 'k', 'p', 'r']]))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert hypercube([
['g', 'f', 'H', 'Y', 'v'],
['z', 'e', 'a', 'P', 'u'],
['s', 'B', 'T', 'e', 'y'],
['k', 'u', 'c', 'R', 't'],
['l', 'O', 'k', 'p', 'r']]) == True
assert hypercube([
['H', 'a', 't', 's', 'E'],
['a', 'Y', 'p', 'u', 'B'],
['a', 'a', 'P', 'y', 'U'],
['x', 'x', 'x', 'E', 'C'],
['z', 'z', 'z', 'z', 'R']]) == False
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 3, 2018
Comments: