Advertisement
J00ker

Untitled

Dec 7th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int st[100], i, n, f = 1;
  6. char a[10][30];
  7.  
  8. void backtrack(int k)
  9. {
  10.     if(k == n+1)
  11.     {
  12.         for(i = 1; i <= n; i++)
  13.             cout << a[st[i]] << " ";
  14.         cout << "\n";
  15.     }
  16.     else
  17.     {
  18.  
  19.         st[k] = 0;
  20.         while(st[k]++ < n)
  21.         {
  22.             f = 1;
  23.             for(i = 1; i < k; i++)
  24.                 if(st[i] == st[k])
  25.                     f = 0;
  26.             if(f) backtrack(k+1);
  27.         }
  28.     }
  29. }
  30.  
  31. int main()
  32. {
  33.     cin >> n; cin.get();
  34.     for(i = 1; i <= n; i++)
  35.     {
  36.         cin.get(a[i], 30);
  37.         cin.get();
  38.     }
  39.     backtrack(1);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement