In this edit I have used the backtracking to reduce the number of objects that are created during recursion.