Created
June 13, 2025 18:40
-
-
Save thinkphp/f67118edf6c7fe02efebe5af4b6daae4 to your computer and use it in GitHub Desktop.
problema 3. domino pieces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
typedef struct { | |
int x1, | |
x2; | |
} Domino; | |
int totalChains = 0; | |
/* | |
piece1: (1,2) | |
piece2: (3,2) | |
piece3: (4,1) | |
piece4: (3,7) | |
piece5: (5,4) | |
(2,1) -> (1,4) -> (4,5) | |
*/ | |
bool canConnect(Domino piece1, Domino piece2) { | |
return (piece1.x1 == piece2.x1) || (piece1.x1 == piece2.x2) || | |
(piece1.x2 == piece2.x1) || (piece1.x2 == piece2.x2); | |
} | |
void printChain(Domino dominoes[], int chain[], int chainSize) { | |
totalChains++; | |
printf("Lantul %d ",totalChains); | |
for(int i = 0; i < chainSize; ++i) { | |
int idx = chain[i]; | |
printf("(%d,%d)", dominoes[idx].x1, dominoes[idx].x2); | |
if(i < chainSize - 1) { | |
printf(" -> "); | |
} | |
} | |
printf("\n"); | |
} | |
void findDominoChains(Domino dominoes[], int n, int m, bool used[], int currentChain[], int chainSize) { | |
if(chainSize == m) { | |
printChain(dominoes, currentChain, chainSize); | |
return; | |
} | |
for(int i = 0; i < n; ++i) { | |
if( used[ i ]) { | |
continue; | |
} | |
bool canAdd = false; | |
if(chainSize == 0) { | |
canAdd = true; | |
} else { | |
int lastIndex = currentChain[chainSize - 1]; | |
canAdd = canConnect(dominoes[lastIndex], dominoes[i]); | |
} | |
if( canAdd ) { | |
used[ i ] = true; | |
currentChain[chainSize] = i; | |
findDominoChains(dominoes, n, m, used, currentChain, chainSize + 1); | |
used[ i ] = false; | |
} | |
} | |
} | |
int main(int argc, char const *argv[]) { | |
int n, m; | |
printf("Introduceti numarul de piese de domino disponibile (n):"); | |
scanf("%d", &n); | |
printf("Introduceti numarul de piese pentru lant (m): "); | |
scanf("%d", &m); | |
if(m > n) { | |
printf("Error: m cannot be > n"); | |
return 1; | |
} | |
if(m <= 0 || n <= 0) { | |
printf("Error: n, m > 0"); | |
return 1; | |
} | |
Domino *dominoes = (Domino*)malloc(n * sizeof(Domino)); | |
printf("Introduceti piesele de domino ca perechi (x1,x2):\n"); | |
for(int i = 0; i < n; ++i) { | |
printf("Piece %d: ", i + 1 ); | |
scanf("%d %d", &dominoes[i].x1, &dominoes[i].x2); | |
} | |
printf("Piesele de domino introduse: \n"); | |
for(int i = 0; i < n; ++i) { | |
printf("Piece %d: (%d, %d)\n", i + 1, dominoes[i].x1, dominoes[i].x2); | |
} | |
bool *used = (bool*)calloc(n, sizeof(bool)); | |
int *currentChain = (int*)malloc(m * sizeof(int)); | |
totalChains = 0; | |
printf("Cautam toate lanturile cu %d piese: \n", m); | |
printf("------------------------------------\n"); | |
findDominoChains(dominoes, n, m, used, currentChain, 0); | |
printf("------------------------------------\n"); | |
if(totalChains == 0){ | |
printf("Nu s-au gasit lanturi valide!\n"); | |
}else { | |
printf("Total lanturi gasite: %d", totalChains); | |
} | |
//free(dominoes); | |
//free(used) | |
//free(currentChain) | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment