st0le
6/20/2013 - 2:31 AM

minhop dfs

minhop dfs

 
def minhops_dfs(lst):
    sz = len(lst)
    min_hop = [0] * sz
    def dfs(index,s,hops,min_hop):
        
        if index >= sz:
            if len(min_hop) > s:
                min_hop[:] = hops
                #min_hop.extend(hops)
        elif s > len(min_hop):
            pass
        else:
            hops.append(lst[index])
            for i in xrange(1,lst[index] + 1):
                dfs(index + i, s + 1,hops,min_hop)
            hops.pop()
    dfs(0,0,[],min_hop)
    return len(min_hop)