Advertisement
J00ker

Bt

Oct 2nd, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. int st[100], n;
  7.  
  8. void Init(int k)
  9. {
  10.     st[k] = 0;
  11. }
  12.  
  13. int Succesor(int k)
  14. {
  15.     if(st[k] < n)
  16.     {
  17.         st[k]++;
  18.         return 1;
  19.     }
  20.     return 0;
  21. }
  22.  
  23. int Valid(int k)
  24. {
  25.     for(int i = 1; i <  k; i++)
  26.         if(st[k] == st[i])
  27.             return 0;
  28.     return 1;
  29. }
  30.  
  31. int Solutie(int k)
  32. {
  33.     if(k == n+1)
  34.         return 1;
  35.     return 0;
  36. }
  37.  
  38. void Tipar()
  39. {
  40.     for(int i = 1; i <= n; i++)
  41.         cout << st[i] << " ";
  42.     cout << "\n";
  43. }
  44.  
  45. void backtrack(int k)
  46. {
  47.     if(Solutie(k))
  48.         Tipar();
  49.     else
  50.     {
  51.         Init(k);
  52.         while(Succesor(k))
  53.             if(Valid(k))
  54.                 backtrack(k+1);
  55.     }
  56. }
  57.  
  58. int main()
  59. {
  60.     cin >> n;
  61.     backtrack(1);
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement