Advertisement
Guest User

Test

a guest
Jan 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. bool findHorizontalWord(char grid[100][100], char *word, int currentRow, int currentColumn, unsigned char layer, int columns) {
  8.     if (word[layer] == '\0')
  9.         return 1;
  10.     else if (currentColumn + layer >= columns || grid[currentRow][currentColumn + layer] != word[layer])
  11.         return 0;
  12.     else
  13.         return findHorizontalWord(grid, word, currentRow, currentColumn, layer + 1, columns);
  14. }
  15.  
  16. bool findVerticalWord(char grid[100][100], char *word, int currentRow, int currentColumn, unsigned char layer, int rows, int columns) {
  17.     if (word[layer] == '\0')
  18.         return 1;
  19.     else if (currentRow + layer >= rows || grid[currentRow + layer][currentColumn] != word[layer])
  20.         return 0;
  21.     else
  22.         return findVerticalWord(grid, word, currentRow, currentColumn, layer + 1, rows, columns);
  23. }
  24.  
  25. unsigned int countWords(char words[50][101], unsigned int numWords, char grid[100][100], unsigned int gridRows, unsigned int gridColumns) {
  26.     if (gridRows > 100 || gridColumns > 100) {
  27.         printf("Bad input!\n");
  28.         exit(1);
  29.     }
  30.  
  31.     int found = 0, result = 0;
  32.     bool checkedWords[50];
  33.     for (int i = 0; i < numWords; ++i)
  34.         checkedWords[i] = false;
  35.  
  36.     for (int i = 0; i < gridRows; ++i)
  37.         for (int j = 0; j < gridColumns; ++j)
  38.             for (int k = 0; k < numWords; ++k)
  39.                 if (grid[gridRows][gridColumns] == words[k][0]) {
  40.                     found += findHorizontalWord(grid, &words[k][0], i, j, 0, gridColumns);
  41.                     found += findVerticalWord(grid, &words[k][0], i, j, 0, gridRows, gridColumns);
  42.                     if (found > 0) {
  43.                         checkedWords[k] = true;
  44.                         ++result;
  45.                     }
  46.                 }
  47.  
  48.     return result;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement