Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int n, a[101], f[101];
  5.  
  6. int check() {
  7. for(int i = 0; i < n; i++)
  8. if(a[i] == i+1) return 1;
  9. return 0;
  10. }
  11.  
  12. void backtrack(int index) {
  13. if(index == n) {
  14. if(check()) {
  15. for(int i = 0; i < n; i++)
  16. cout << a[i] << " ";
  17. cout << endl;
  18. }
  19. return;
  20. }
  21. for(int i = 1; i <= n; i++) {
  22. if(f[i] == 0) {
  23. a[index] = i;
  24. f[i] = 1;
  25. backtrack(index + 1);
  26. f[i] = 0;
  27. }
  28. }
  29. }
  30.  
  31. int main() {
  32. cin >> n;
  33.  
  34. backtrack(0);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement