Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Recurse solution in Clear category for Stacking Cubes by U.V
from itertools import product
def stacking_cubes(cubes):
heights = [i[2] for i in cubes]
N = len(cubes)
# squares, occupied by each cube
sCubes = [set(product(range(x, x+r), range(y, y+r))) for x, y, r in cubes]
# cubes, having common parts
commonCubes = {i: [ j for j in range(N) if len(sCubes[i].intersection(sCubes[j])) and i != j]
for i in range(N) }
# Recursive find height of tower
def getHeight(used, nod):
return heights[nod] + max([getHeight(used+[nod], nod) for nod in commonCubes[nod] if nod not in used]+[0])
return max(getHeight([i], i) for i in range(N))
Jan. 15, 2023