Templates

1def dfs(state):
2    res = []
3    if is_solution(state):
4        res.append(state[:]) # e.g. add a copy of the state to final result list
5        return
6
7    for choice in choices:
8        if valid(choice):
9            state.add(choice) # make move
10            dfs(state)
11            state.remove(choice) # backtrack
12dfs(root, [])
13
Read the article about this template.