Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char** initMat(int n) {
- char **a = NULL;
- if((a=(char**)malloc(n*sizeof(char*)))==NULL){
- printf("memorie insuficienta\n");
- exit(EXIT_FAILURE);
- }
- // alocare linii
- for(int i=0;i<n;i++){
- if((a[i]=(char*)malloc(n*sizeof(char)))==NULL){
- for(i--;i>=0;i--)free(a[i]); // elibereaza liniile alocate anterior
- free(a); // elibereaza vectorul de pointeri
- printf("memorie insuficienta\n");
- exit(EXIT_FAILURE);
- }
- }
- return a;
- }
- void readMat(char **a, int n) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- scanf("%c", &a[i][j]);
- if (a[i][j] == '\n' || a[i][j] == ' ') {
- scanf("%c", &a[i][j]);
- }
- }
- }
- }
- void freeMat(char **a, int n) {
- for (int i = 0; i < n; i++) {
- free(a[i]);
- }
- free(a);
- }
- void checkStr(char *s) {
- if (s == NULL) {
- perror(NULL);
- exit(-1);
- }
- }
- int checkRight(char **a, char s[], int n, int l, int c) {
- int cc = c;
- char *aux = calloc(sizeof(char), n + 1);
- for (;c < n; c++) {
- strncat(aux, &a[l][c], 1);
- if (strcmp(aux, s) == 0) {
- free(aux);
- printf("de la linia %d col %d spre dreapta\n", l, cc);
- return 1;
- }
- }
- free(aux);
- return 0;
- }
- int checkLeft(char **a, char s[], int n, int l, int c) {
- int cc = c;
- char *aux = calloc(sizeof(char), n + 1);
- for (;c >= 0; c--) {
- strncat(aux, &a[l][c], 1);
- if (strcmp(aux, s) == 0) {
- free(aux);
- printf("de la linia %d col %d spre stanga\n", l, cc);
- return 1;
- }
- }
- free(aux);
- return 0;
- }
- int checkUp(char **a, char s[], int n, int l, int c) {
- int ll = l;
- char *aux = calloc(sizeof(char), n + 1);
- for (; l >= 0; l--) {
- strncat(aux, &a[l][c], 1);
- if (strcmp(aux, s) == 0) {
- free(aux);
- printf("de la linia %d col %d in sus\n", ll, c);
- return 1;
- }
- }
- free(aux);
- return 0;
- }
- int checkDown(char **a, char s[], int n, int l, int c) {
- int ll = l;
- char *aux = calloc(sizeof(char), n + 1);
- for (; l < n; l++) {
- strncat(aux, &a[l][c], 1);
- if (strcmp(aux, s) == 0) {
- free(aux);
- printf("de la linia %d col %d in jos\n", ll, c);
- return 1;
- }
- }
- free(aux);
- return 0;
- }
- int main(void) {
- char **a;
- int n;
- scanf("%d", &n);
- a = initMat(n);
- readMat(a, n);
- char *word = NULL;
- checkStr(word = calloc(sizeof(char), n + 1));
- getchar();
- checkStr(fgets(word, n + 1, stdin));
- word[strlen(word) - 1] = 0;
- int ex = 0;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- ex |= checkRight(a, word, n, i, j);
- ex |= checkLeft(a, word, n, i, j);
- ex |= checkDown(a, word, n, i, j);
- ex |= checkUp(a, word, n, i, j);
- }
- }
- if (!ex) {
- puts("Nu exista solutie");
- }
- free(word);
- freeMat(a, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement