use backtrack to generate the permutations