Advertisement
clucasg

q2.c

Oct 29th, 2020
1,982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5.  
  6. void imprimePalavra(char *c, int salto, int tam)
  7. {
  8.     if (c == NULL)
  9.     {
  10.         printf("Palavra nao encontrada\n");
  11.         return;
  12.     }
  13.     for (int i = 0; i < tam; i++)
  14.     {
  15.         printf("%c", *c);
  16.         c += salto;
  17.     }
  18.     printf(", salto:%d\n", salto);
  19. }
  20.  
  21. int localizaPalavra(char palavra[], char tabuleiro[10][10], char **primeiraLetra, int *salto)
  22. {
  23.     int tamanho = strlen(palavra);
  24.     int i, j;
  25.  
  26.     // Novo tabuleiro e palavra todos/toda minusculo(a)
  27.     char lowerTabuleiro[10][10];
  28.     for (i = 0; i < 10; i++)
  29.     {
  30.         for (j = 0; j < 10; j++)
  31.         {
  32.             lowerTabuleiro[i][j] = tolower(tabuleiro[i][j]);
  33.         }
  34.     }
  35.     char lowerPalavra[10];
  36.     for (i = 0; i < 10; i++)
  37.     {
  38.         lowerPalavra[i] = tolower(palavra[i]);
  39.     }
  40.  
  41.     // Booleans conditions
  42.  
  43.     for (i = 0; i < 10; i++)
  44.     {
  45.         for (j = 0; j < 10; j++)
  46.         {
  47.             bool achouLetra = false;
  48.             // printf("Verificando %d %d (%c) \n", i, j, tabuleiro[i][j]);
  49.             if (lowerTabuleiro[i][j] == lowerPalavra[0])
  50.             {
  51.                 *primeiraLetra = &tabuleiro[i][j];
  52.                 // printf("Achou a primeira letra em %d %d\n", i, j);
  53.                 bool direita = (j + tamanho <= 10) ? true : false;
  54.                 bool esquerda = (j >= tamanho - 1) ? true : false;
  55.                 bool baixo = (i + tamanho <= 10) ? true : false;
  56.                 bool cima = (i >= tamanho - 1) ? true : false;
  57.                 // Verifica a Direita
  58.                 if (direita)
  59.                 {
  60.                     bool achou = false;
  61.                     // puts("Direita");
  62.                     for (int x = 1; x < tamanho; x++)
  63.                     {
  64.                         // // printf("Verificando %d %d (%c) == %c \n", i, j + x, tabuleiro[i][j + x], palavra[x]);
  65.                         if (lowerTabuleiro[i][j + x] != lowerPalavra[x])
  66.                         {
  67.                             // printf("eh, achou\n");
  68.                             achou = true;
  69.                             break;
  70.                         }
  71.                     }
  72.                     if (!achou)
  73.                     {
  74.                         // puts("como nao achou, fim");
  75.                         *salto = 1;
  76.                         return 0;
  77.                     }
  78.                 }
  79.                 // Verifica a Esquerda
  80.                 if (esquerda)
  81.                 {
  82.                     bool achou = false;
  83.                     // puts("Esquerda");
  84.                     for (int x = 1; x < tamanho; x++)
  85.                     {
  86.                         // printf("Verificando %d %d (%c) == %c \n", i, j - x, tabuleiro[i][j - x], palavra[x]);
  87.                         if (lowerTabuleiro[i][j - x] != lowerPalavra[x])
  88.                         {
  89.                             // printf("eh, achou\n");
  90.                             achou = true;
  91.                             break;
  92.                         }
  93.                     }
  94.                     if (!achou)
  95.                     {
  96.                         // puts("como nao achou, fim");
  97.                         *salto = -1;
  98.                         return 0;
  99.                     }
  100.                 }
  101.                 // Verificando em cima
  102.                 if (cima)
  103.                 {
  104.                     bool achou = false;
  105.                     // puts("Cima");
  106.                     for (int x = 1; x < tamanho; x++)
  107.                     {
  108.                         // printf("Verificando %d %d (%c) == %c \n", i - x, j, tabuleiro[i - x][j], palavra[x]);
  109.                         if (lowerTabuleiro[i - x][j] != lowerPalavra[x])
  110.                         {
  111.                             // printf("eh, achou\n");
  112.                             achou = true;
  113.                             break;
  114.                         }
  115.                     }
  116.                     if (!achou)
  117.                     {
  118.                         // puts("como nao achou, fim");
  119.                         *salto = -10;
  120.                         return 0;
  121.                     }
  122.                 }
  123.                 // Verificando Baixo
  124.                 if (baixo)
  125.                 {
  126.                     bool achou = false;
  127.                     // puts("Baixo");
  128.                     for (int x = 1; x < tamanho; x++)
  129.                     {
  130.                         // printf("Verificando %d %d (%c) == %c \n", i + x, j, tabuleiro[i + x][j], palavra[x]);
  131.                         if (lowerTabuleiro[i + x][j] != lowerPalavra[x])
  132.                         {
  133.                             // printf("eh, achou\n");
  134.                             achou = true;
  135.                             break;
  136.                         }
  137.                     }
  138.                     if (!achou)
  139.                     {
  140.                         // puts("como nao achou, fim");
  141.                         *salto = 10;
  142.                         return 0;
  143.                     }
  144.                 }
  145.                 // Verifica Diagonal 1 "/" cima->baixo
  146.                 if (baixo && esquerda)
  147.                 {
  148.                     bool achou = false;
  149.                     // puts("Diagonal 1 \"/\" cima->baixo");
  150.                     for (int x = 1; x < tamanho; x++)
  151.                     {
  152.                         // printf("Verificando %d %d (%c) == %c \n", i + x, j - x, tabuleiro[i + x][j - x], palavra[x]);
  153.                         if (lowerTabuleiro[i + x][j - x] != lowerPalavra[x])
  154.                         {
  155.                             // printf("eh, achou\n");
  156.                             achou = true;
  157.                             break;
  158.                         }
  159.                     }
  160.                     if (!achou)
  161.                     {
  162.                         // puts("como nao achou, fim");
  163.                         *salto = 9;
  164.                         return 0;
  165.                     }
  166.                 }
  167.                 // Verifica Diagonal 2 "\" cima->baixo
  168.                 if (baixo && direita)
  169.                 {
  170.                     bool achou = false;
  171.                     // puts("Diagonal 2 \"\\\" cima->baixo");
  172.                     for (int x = 1; x < tamanho; x++)
  173.                     {
  174.                         // printf("Verificando %d %d (%c) == %c \n", i + x, j + x, tabuleiro[i + x][j + x], palavra[x]);
  175.                         if (lowerTabuleiro[i + x][j + x] != lowerPalavra[x])
  176.                         {
  177.                             // printf("eh, achou\n");
  178.                             achou = true;
  179.                             break;
  180.                         }
  181.                     }
  182.                     if (!achou)
  183.                     {
  184.                         // puts("como nao achou, fim");
  185.                         *salto = 11;
  186.                         return 0;
  187.                     }
  188.                 }
  189.                 // Verifica Diagonal 3 "/" baixo->cima
  190.                 if (cima && direita)
  191.                 {
  192.                     bool achou = false;
  193.                     // puts("Diagonal 3 \"/\" baixo->cima");
  194.                     for (int x = 1; x < tamanho; x++)
  195.                     {
  196.                         // printf("Verificando %d %d (%c) == %c \n", i - x, j + x, tabuleiro[i - x][j + x], palavra[x]);
  197.                         if (lowerTabuleiro[i - x][j + x] != lowerPalavra[x])
  198.                         {
  199.                             // printf("eh, achou\n");
  200.                             achou = true;
  201.                             break;
  202.                         }
  203.                     }
  204.                     if (!achou)
  205.                     {
  206.                         // puts("como nao achou, fim");
  207.                         *salto = -9;
  208.                         return 0;
  209.                     }
  210.                 }
  211.                 // Verifica Diagonal 4 "\" baixo->cima
  212.                 if (cima && esquerda)
  213.                 {
  214.                     bool achou = false;
  215.                     // puts("Diagonal 4 \"\\\" baixo->cima");
  216.                     for (int x = 1; x < tamanho; x++)
  217.                     {
  218.                         // printf("Verificando %d %d (%c) == %c \n", i - x, j - x, tabuleiro[i - x][j - x], palavra[x]);
  219.                         if (lowerTabuleiro[i - x][j - x] != lowerPalavra[x])
  220.                         {
  221.                             // printf("eh, achou\n");
  222.                             achou = true;
  223.                             break;
  224.                         }
  225.                     }
  226.                     if (!achou)
  227.                     {
  228.                         // puts("como nao achou, fim");
  229.                         *salto = -11;
  230.                         return 0;
  231.                     }
  232.                 }
  233.             }
  234.         }
  235.     }
  236.     *primeiraLetra = NULL;
  237. }
  238.  
  239. int main()
  240. {
  241.     // Palavras a serem buscadas
  242.     char palavras[5][11];
  243.     int n;
  244.     scanf("%d", &n);
  245.     for (int i = 0; i < n; i++)
  246.     {
  247.         scanf("%s", &palavras[i]);
  248.     }
  249.     // Preenche o Caça-Palavras
  250.     char tabuleiro[10][10];
  251.     for (int i = 0; i < 10; i++)
  252.     {
  253.         char linha[11];
  254.  
  255.         scanf("%s", &linha);
  256.         for (int j = 0; j < 10; j++)
  257.         {
  258.             tabuleiro[i][j] = linha[j];
  259.         }
  260.     }
  261.  
  262.     for (int i = 0; i < n; i++)
  263.     {
  264.         int salto;
  265.         char *primeiraletra = NULL;
  266.         localizaPalavra(palavras[i], tabuleiro, &primeiraletra, &salto);
  267.         imprimePalavra(primeiraletra, salto, strlen(palavras[i]));
  268.     }
  269.  
  270.     return 0;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement