Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #pragma warning(disable:4996)
- int v[100];
- int cipherSize;
- int solutionNumber;
- int maxSolutionElementValue = 4;
- /*
- int a = 8;
- int b = 0;
- int c = 9;
- int d = 2;
- */
- void Init(int i)
- {
- v[i] = 0;
- //iniţializează/resetează, valoarea din
- // vârful stivei
- }
- int Succesor(int k) {
- if (v[k] < maxSolutionElementValue)
- { // se poate creşte valoarea din vârf
- v[k]++; // se incrementează valoarea din vârf
- return 1; // funcţia a avut success
- }
- else // nu se poate creşte valoarea din vârf
- {
- return 0;
- }
- }
- int Valid(k) {
- int i = 0;
- for (i = 1; i < k; i++) // verifică dacă elementul din
- {
- if (v[i] == v[k])
- {
- return 0; // vârf este diferit de
- }
- // elemente precedente din stivă
- }
- return 1;
- }
- int Solution(int i)
- {
- return (i == cipherSize);
- }
- void Print() {
- int i;
- printf("%d : ", ++solutionNumber);
- for (i = 1; i <= cipherSize; i++)
- {
- printf("%d ", v[i]);
- }
- printf("\n");
- }
- void Back() {
- int index, isS, isV;
- index = 1;
- Init(index);
- while (index > 0)
- { // cât timp stiva nu e vidă
- isS = 0;
- isV = 0;
- if (index <= cipherSize) // nu face sens depăşirea nivelului n în stivă
- {
- do {
- // repetă cât timp...
- isS = Succesor(index);
- if (isS)
- {
- isV = Valid(index);
- }
- } while (isS && !isV);
- }
- // ...există succesor dar nu este valid
- if (isS) //este succesor si este valid
- {
- if (Solution(index)) // verifică candidatul la soluţie
- {
- Print(); // afişează soluţia
- }
- else
- { // dacă nu este soluţie
- index++;
- Init(index); // creşte vârful stivei şi iniţializează
- }
- }
- else // nu există succesor pt. valoarea curentă din stivă
- {
- index--; // -> se coboară o poziţie în stivă
- }
- }
- }
- void main()
- {
- printf("Aranjamente de 4 luate cate 2:\n");
- cipherSize = 2;
- Back();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement