Advertisement
MouseyN1

Permutari multime backtracking

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