Advertisement
Guest User

batu lu boghy

a guest
Oct 16th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ofstream g("bara.out");
  7.  
  8. int st[100], n, L;
  9. int bara[100];
  10. int este = 0, s=0;
  11.  
  12. int surjectiva(int k)
  13. {
  14.     bool ok;
  15.     for(int i = 1; i <= n; i++)
  16.     {
  17.         ok = false;
  18.         for(int j = 1; j <= k; j++)
  19.             if(bara[i] == bara[st[j]])
  20.                 ok = true;
  21.         if(!ok)
  22.             return 0;
  23.     }
  24.     return 1;
  25. }
  26.  
  27. void citire()
  28. {
  29.     ifstream f("bara.in");
  30.     f >> L >> n;
  31.     for(int i = 1; i <= n; i++)
  32.         f >> bara[i];
  33.     f.close();
  34. }
  35.  
  36. void init(int k)
  37. {
  38.     if(k == 1)
  39.         st[k] = 0;
  40.     else
  41.         st[k] = st[k-1] - 1;
  42. }
  43.  
  44. int succesor(int k)
  45. {
  46.     if(st[k] < n)
  47.     {
  48.         st[k]++;
  49.         return 1;
  50.     }
  51.     else
  52.     {
  53.         s -= bara[st[k-1]];
  54.         return 0;
  55.     }
  56. }
  57.  
  58. int valid(int k)
  59. {
  60.     if(s + bara[st[k]] <= L)
  61.     {
  62.         s += bara[st[k]];
  63.         return 1;
  64.     }
  65.     return 0;
  66. }
  67.  
  68. int solutie(int k)
  69. {
  70.     return s == L;
  71. }
  72.  
  73. void tipar(int k)
  74. {
  75.     este = 1;
  76.     for(int i = 1; i < k; i++)
  77.     {
  78.         for(int j = 1; j < bara[st[i]]; j++)
  79.             g << "-";
  80.         g << "|";
  81.     }
  82.     for(int i = 1; i<bara[st[k]]; i++)
  83.         g << "-";
  84.     g << "\n\n";
  85.     s -= bara[st[k]];
  86. }
  87.  
  88. void bkt(int k)
  89. {
  90.     init(k);
  91.     while(succesor(k))
  92.     {
  93.         if(valid(k))
  94.         {
  95.             if(solutie(k) && surjectiva(k))
  96.             {
  97.                 tipar(k);
  98.             }
  99.             else
  100.             {
  101.                 bkt(k+1);
  102.             }
  103.         }
  104.     }
  105. }
  106.  
  107. int main()
  108. {
  109.     citire();
  110.     bkt(1);
  111.     if(!este)
  112.         cout << "Imposibil";
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement