// Function to check for surrounded tokens. int checkIfTokenSurrounded(int x,int y,int value) { // Set placeholder grid. placeholder[x][y] = 1; // Check grid space Above. // In each check, look at placeholder grid // To keep track of already visited grid spaces // Also check for grid boundaries. if(x != 0 && placeholder[x-1][y] != 1) if(checkSpot(value,x-1,y) == 0) return 0; // Check grid space Below. if(x != 8 && placeholder[x+1][y] != 1) if(checkSpot(value,x+1,y) == 0) return 0; // Check grid space to the left. if(y != 0 && placeholder[x][y-1] != 1) if(checkSpot(value,x,y-1) == 0) return 0; // Check grid space to the right. if(y != 8 && placeholder[x][y+1] != 1) if(checkSpot(value,x,y+1) == 0) return 0; return 1; }
//runs a dfs to check word combinations. After a combination is 3 characters or longer, it sends it to isValidWord and prints it if that returns 1. void checkSpot(int x, int y, int **visited, char** board, char *tempWord,trie *dictionary, int counter){ int i; tempWord[counter - 1] = board[x][y]; tempWord[counter] = '\0'; for (i = 0; i < DIRECTIONS; ++i) { if(isValid(x+dx[i], y+dy[i]) && !visited[x+dx[i]][y+dy[i]]){ if(!isValidPrefix(dictionary,tempWord,counter)){ return; } if(counter >= 3){ if(isValidWord(dictionary,tempWord)){ printf("%s\n", tempWord); } } visited[x][y] = 1; checkSpot(x + dx[i], y + dy[i], visited, board, tempWord, dictionary, counter + 1); visited[x][y] = 0; } } }
int main(){ int games, i,j,k; scanf("%d", &games); trie * dictionary = malloc(sizeof(trie)); for (j = 0; j < 26; ++j) { dictionary->nextLetter[j] = NULL; } dictionary = fillDictionary(dictionary); for (i = 0; i < games; ++i) { char** board = getInput(); int ** visited = malloc(sizeof(int **)); for (j = 0; j < 4; ++j) { visited[j] = calloc(4, sizeof(int *)); } printf("Words for game #%d:\n", i + 1); //Uses all 16 spots as a starting point. malloc is * 16 because the max word size is 16. char* tempWord = malloc(sizeof(char) * 16); for (j = 0; j < 4; ++j) { for (k = 0; k < 4; ++k) { visited[j][k] = 1; checkSpot(j,k,visited,board,tempWord,dictionary,1); visited[j][k] = 0; } } free(tempWord); free(visited); free(board); printf("\n\n"); } freeDictionary(dictionary); return 0; }