Advertisement
J00ker

Untitled

Oct 2nd, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  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.     {
  27.         if(st[k] == st[i])
  28.             return 0;
  29.         if(abs(double(k-i)) < 1 )
  30.             return 0;
  31.     }
  32.     return 1;
  33. }
  34.  
  35. int Solutie(int k)
  36. {
  37.     if(k == n+1)
  38.         return 1;
  39.     return 0;
  40. }
  41.  
  42. void Tipar()
  43. {
  44.     for(int i = 1; i <= n; i++)
  45.         cout << "(" << i << ", " << st[i] << ")";
  46.     cout << "\n";
  47. }
  48.  
  49. void backtrack(int k)
  50. {
  51.     if(Solutie(k))
  52.         Tipar();
  53.     else
  54.     {
  55.         Init(k);
  56.         while(Succesor(k))
  57.             if(Valid(k))
  58.                 backtrack(k+1);
  59.     }
  60. }
  61.  
  62. int main()
  63. {
  64.     cin >> n;
  65.     backtrack(1);
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement