Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- bool findHorizontalWord(char grid[100][100], char *word, int currentRow, int currentColumn, unsigned char layer, int columns) {
- if (word[layer] == '\0')
- return 1;
- else if (currentColumn + layer >= columns || grid[currentRow][currentColumn + layer] != word[layer])
- return 0;
- else
- return findHorizontalWord(grid, word, currentRow, currentColumn, layer + 1, columns);
- }
- bool findVerticalWord(char grid[100][100], char *word, int currentRow, int currentColumn, unsigned char layer, int rows, int columns) {
- if (word[layer] == '\0')
- return 1;
- else if (currentRow + layer >= rows || grid[currentRow + layer][currentColumn] != word[layer])
- return 0;
- else
- return findVerticalWord(grid, word, currentRow, currentColumn, layer + 1, rows, columns);
- }
- unsigned int countWords(char words[50][101], unsigned int numWords, char grid[100][100], unsigned int gridRows, unsigned int gridColumns) {
- if (gridRows > 100 || gridColumns > 100) {
- printf("Bad input!\n");
- exit(1);
- }
- int found = 0, result = 0;
- bool checkedWords[50];
- for (int i = 0; i < numWords; ++i)
- checkedWords[i] = false;
- for (int i = 0; i < gridRows; ++i)
- for (int j = 0; j < gridColumns; ++j)
- for (int k = 0; k < numWords; ++k)
- if (grid[gridRows][gridColumns] == words[k][0]) {
- found += findHorizontalWord(grid, &words[k][0], i, j, 0, gridColumns);
- found += findVerticalWord(grid, &words[k][0], i, j, 0, gridRows, gridColumns);
- if (found > 0) {
- checkedWords[k] = true;
- ++result;
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement