Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
breadth-first search solution in Speedy category for Fast Train by David_Jones
from collections import deque
def fast_train(sections):
V_max = sum(([v_max] * d for d, v_max in sections), [])
queue = deque([(1,)])
while queue:
V = queue.popleft()
v, x = V[-1], sum(V)
if v == 1 and x == len(V_max):
return len(V)
for v_next in (v+1, v, v-1):
if (
0 < v_next <= len(V_max)-x
and all(v_next <= V_max[x+i] for i in range(v_next))
):
queue.append(V+(v_next,))
July 1, 2019