A new backtracking algorithm is developed for generating classes of permutations, that are invariant under the group G4 of rigid motions of the square generated by reflections about the horizontal and vertical axes. Special cases give a new algorithm for generating solutions of the classical n-queens problem, as well as a new algorithm for generating Costas sequences, which are used in encoding radar and sonar signals. Parallel implementations of this latter algorithm have yielded new Costas sequences for length n, 19n24.