Code template
ans = [] def backtrack(state): if is_valid(state): ans.append(state) # return for candidate in get_candidates(state): new_state = state + candidate backtrack(new_state)