• + 1 comment

    Dynamic programming (or memoization) and indeterminism!

    If we suppose n_ways(A, d) to be a function that returns the total number of distinct ways in distance d from ANY house in a set A, we can compute it recursively:

    n_ways(A, d) = n_ways(reach(A, 0), d-1) + n_ways(reach(A, 1), d-1)
    

    where reach(A, c) gives the set of all houses that can be reached from ANY house in A with given clue (c = 0 or 1).

    Then, memoizing n_ways() based on different A and d can boost the performance.