Advertisement
STANAANDREY

tp1 ex 2

Feb 21st, 2023
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char** initMat(int n) {
  6.   char **a = NULL;
  7.   if((a=(char**)malloc(n*sizeof(char*)))==NULL){
  8.     printf("memorie insuficienta\n");
  9.     exit(EXIT_FAILURE);
  10.   }
  11.   // alocare linii
  12.   for(int i=0;i<n;i++){
  13.     if((a[i]=(char*)malloc(n*sizeof(char)))==NULL){
  14.       for(i--;i>=0;i--)free(a[i]); // elibereaza liniile alocate anterior
  15.       free(a); // elibereaza vectorul de pointeri
  16.       printf("memorie insuficienta\n");
  17.       exit(EXIT_FAILURE);
  18.     }
  19.   }
  20.   return a;
  21. }
  22.  
  23. void readMat(char **a, int n) {
  24.   for (int i = 0; i < n; i++) {
  25.     for (int j = 0; j < n; j++) {
  26.       scanf("%c", &a[i][j]);
  27.       if (a[i][j] == '\n' || a[i][j] == ' ') {
  28.     scanf("%c", &a[i][j]);
  29.       }
  30.     }
  31.   }
  32. }
  33.  
  34. void freeMat(char **a, int n) {
  35.   for (int i = 0; i < n; i++) {
  36.     free(a[i]);
  37.   }
  38.   free(a);
  39. }
  40.  
  41. void checkStr(char *s) {
  42.   if (s == NULL) {
  43.     perror(NULL);
  44.     exit(-1);
  45.   }
  46. }
  47.  
  48. int checkRight(char **a, char s[], int n, int l, int c) {
  49.   int cc = c;
  50.   char *aux = calloc(sizeof(char), n + 1);
  51.   for (;c < n; c++) {
  52.     strncat(aux, &a[l][c], 1);
  53.     if (strcmp(aux, s) == 0) {
  54.       free(aux);
  55.       printf("de la linia %d col %d spre dreapta\n", l, cc);
  56.       return 1;
  57.     }
  58.   }
  59.   free(aux);
  60.   return 0;
  61. }
  62.  
  63. int checkLeft(char **a, char s[], int n, int l, int c) {
  64.   int cc = c;
  65.   char *aux = calloc(sizeof(char), n + 1);
  66.   for (;c >= 0; c--) {
  67.     strncat(aux, &a[l][c], 1);
  68.     if (strcmp(aux, s) == 0) {
  69.       free(aux);
  70.       printf("de la linia %d col %d spre stanga\n", l, cc);
  71.       return 1;
  72.     }
  73.   }
  74.   free(aux);
  75.   return 0;
  76. }
  77.  
  78. int checkUp(char **a, char s[], int n, int l, int c) {
  79.   int ll = l;
  80.   char *aux = calloc(sizeof(char), n + 1);
  81.   for (; l >= 0; l--) {
  82.     strncat(aux, &a[l][c], 1);
  83.     if (strcmp(aux, s) == 0) {
  84.       free(aux);
  85.       printf("de la linia %d col %d in sus\n", ll, c);
  86.       return 1;
  87.     }
  88.   }
  89.   free(aux);
  90.   return 0;
  91. }
  92.  
  93. int checkDown(char **a, char s[], int n, int l, int c) {
  94.   int ll = l;
  95.   char *aux = calloc(sizeof(char), n + 1);
  96.   for (; l < n; l++) {
  97.     strncat(aux, &a[l][c], 1);
  98.     if (strcmp(aux, s) == 0) {
  99.       free(aux);
  100.       printf("de la linia %d col %d in jos\n", ll, c);
  101.       return 1;
  102.     }
  103.   }
  104.   free(aux);
  105.   return 0;
  106. }
  107.  
  108. int main(void) {
  109.   char **a;
  110.   int n;
  111.   scanf("%d", &n);
  112.   a = initMat(n);
  113.   readMat(a, n);
  114.   char *word = NULL;
  115.   checkStr(word = calloc(sizeof(char), n + 1));
  116.   getchar();
  117.   checkStr(fgets(word, n + 1, stdin));
  118.   word[strlen(word) - 1] = 0;
  119.  
  120.   int ex = 0;
  121.   for (int i = 0; i < n; i++) {
  122.     for (int j = 0; j < n; j++) {
  123.       ex |= checkRight(a, word, n, i, j);
  124.       ex |= checkLeft(a, word, n, i, j);
  125.       ex |= checkDown(a, word, n, i, j);
  126.       ex |= checkUp(a, word, n, i, j);
  127.     }
  128.   }
  129.  
  130.   if (!ex) {
  131.     puts("Nu exista solutie");
  132.   }
  133.  
  134.   free(word);
  135.   freeMat(a, n);
  136.   return 0;
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement