solve it by permutation; also by STL next_permutation algorithm