Advertisement
Patey

Untitled

Mar 26th, 2021
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #pragma warning(disable:4996)
  4.  
  5. int v[100];
  6. int cipherSize;
  7. int solutionNumber;
  8. int maxSolutionElementValue = 4;
  9.  
  10. /*
  11. int a = 8;
  12. int b = 0;
  13. int c = 9;
  14. int d = 2;
  15. */
  16.  
  17. void Init(int i)
  18. {
  19.     v[i] = 0;
  20.     //iniţializează/resetează, valoarea din
  21.     // vârful stivei
  22. }
  23.  
  24. int Succesor(int k) {
  25.     if (v[k] < maxSolutionElementValue)
  26.     { // se poate creşte valoarea din vârf
  27.         v[k]++; // se incrementează valoarea din vârf
  28.         return 1; // funcţia a avut success
  29.     }
  30.     else // nu se poate creşte valoarea din vârf
  31.     {
  32.         return 0;
  33.     }
  34. }
  35.  
  36. int Valid(k) {
  37.     int i = 0;
  38.     for (i = 1; i < k; i++) // verifică dacă elementul din
  39.     {
  40.         if (v[i] == v[k])
  41.         {
  42.             return 0; // vârf este diferit de
  43.         }
  44.         // elemente precedente din stivă
  45.     }
  46.     return 1;
  47. }
  48.  
  49.  
  50. int Solution(int i)
  51. {
  52.     return (i == cipherSize);
  53. }
  54.  
  55. void Print() {
  56.     int i;
  57.     printf("%d : ", ++solutionNumber);
  58.     for (i = 1; i <= cipherSize; i++)
  59.     {
  60.         printf("%d ", v[i]);
  61.     }
  62.     printf("\n");
  63. }
  64.  
  65. void Back() {
  66.     int index, isS, isV;
  67.     index = 1;
  68.     Init(index);
  69.     while (index > 0)
  70.     { // cât timp stiva nu e vidă
  71.         isS = 0;
  72.         isV = 0;
  73.         if (index <= cipherSize) // nu face sens depăşirea nivelului n în stivă
  74.         {
  75.             do {
  76.                 // repetă cât timp...
  77.                 isS = Succesor(index);
  78.                 if (isS)
  79.                 {
  80.                     isV = Valid(index);
  81.                 }
  82.             } while (isS && !isV);
  83.         }
  84.         // ...există succesor dar nu este valid
  85.         if (isS) //este succesor si este valid
  86.         {
  87.             if (Solution(index)) // verifică candidatul la soluţie
  88.             {
  89.                 Print(); // afişează soluţia
  90.             }
  91.             else
  92.             {   // dacă nu este soluţie
  93.                 index++;
  94.                 Init(index); // creşte vârful stivei şi iniţializează
  95.             }
  96.         }
  97.         else // nu există succesor pt. valoarea curentă din stivă
  98.         {
  99.             index--; // -> se coboară o poziţie în stivă
  100.         }
  101.     }
  102. }
  103.  
  104. void main()
  105. {
  106.     printf("Aranjamente de 4 luate cate 2:\n");
  107.     cipherSize = 2;
  108.     Back();
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement