Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
19-liner: correct solution in Clear category for Graphical Key by przemyslaw.daniel
def is_valid(points, last):
stack = [(0, 0, set(points))]
while stack:
x, y, left = stack.pop()
if not left and (x, y) == last: return True
stack += [(i, j, left-{(i, j)}) for i, j in left
if abs(x+y*1j-i-j*1j) < 2]
def g_key(data, number):
from itertools import combinations as comb
height, width, best = len(data), len(data[0]), 0
valid = {(x, y) for x in range(height) for y in range(width)}
candidates = comb(valid-{(0, 0), (height-1, width-1)}, number-2)
for candidate in candidates:
candidate += ((height-1, width-1),)
current = data[0][0]+sum(data[x][y] for x, y in candidate)
if current > best and is_valid(candidate, (height-1, width-1)):
best = current
return best
Sept. 27, 2018