#include <stdio.h> #include <stdlib.h> #include <string.h> #define TRUE 1 #define FALSE 0 /* #define DBG */ #ifdef DBG #define DEBUG if (TRUE) #else #define DEBUG if (FALSE) #endif #define CL2d(a,b,x,y) memset(a, b, sizeof(a[0][0])*x*y) /* GLOBALS */ char boggleboard[10][10]; char dictionary[201][26]; char solutions[201][26]; int n; int sz; int xs[] = {1, 1, 0, -1, -1, -1, 0, 1}; int ys[] = {0, 1, 1, 1, 0, -1, -1, -1}; /* END GLOBALS */ int cmp(const void* a, const void* b) { const char* str1 = (const char*)a; const char* str2 = (const char*)b; return strcmp(str1, str2); } int get_input() { scanf("%d ", &sz); if (sz == 0) return FALSE; int i; for (i = 0; i < sz; ++i) scanf("%25s ", dictionary[i]); /*DEBUG { int j; for (j = 0; j < sz; ++j) printf("%s\n", dictionary[j]); }*/ return TRUE; } int find_potential(char board[][10], char* word, int pos_in_w, int x, int y) { if (pos_in_w == strlen(word)) return TRUE; char temp = board[x][y]; board[x][y] = 0; int decision = FALSE; DEBUG { printf("word: %s\tpos_in_w: %d\tx: %d\ty: %d\n", word, pos_in_w, x, y); } int i, newx, newy; for (i = 0; i < 8; ++i) { if (decision) break; newx = x+xs[i]; newy = y+ys[i]; if (board[newx][newy] == 'q') { if ((strlen(word+pos_in_w) > 1) && (word[pos_in_w] == 'q') && (word[pos_in_w+1] == 'u')) decision = find_potential(board, word, pos_in_w+2, newx, newy); } else if (board[newx][newy] == word[pos_in_w]) decision = find_potential(board, word, pos_in_w+1, newx, newy); } board[x][y] = temp; return decision; } void process() { int i, j, k, solved_num; scanf("%d ", &n); while (n != 0) { CL2d(boggleboard, 0, 10, 10); CL2d(solutions, 0, 201, 26); solved_num = 0; for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) { scanf("%c", &boggleboard[i][j]); } scanf(" "); } /*DEBUG { for (i = 0; i <= n+1; ++i) { for (j = 0; j <= n+1; ++j) { printf("%c", boggleboard[i][j]); } puts(""); } }*/ /*ACTUAL PROCESSING*/ /* go through each word */ char curword[26]; char newb[10][10]; /* new board */ int found; for (i = 0; i < sz; ++i) { strncpy(curword, dictionary[i], 26); /* go through each square and compare */ for (j = 1; j <= n; ++j) { for (k = 1; k <= n; ++k) { found = 0; if (boggleboard[j][k] == 'q') { if ((strlen(curword) > 1) && (curword[0] == 'q') && (curword[1] == 'u')) { memcpy(newb, boggleboard, sizeof(boggleboard[0][0])*10*10); found = find_potential(newb, curword, 2, j, k); } } else if (boggleboard[j][k] == curword[0]) { memcpy(newb, boggleboard, sizeof(boggleboard[0][0])*10*10); found = find_potential(newb, curword, 1, j, k); } DEBUG { printf("found: %d\n", found); } if (found) { strncpy(solutions[solved_num], curword, 26); solved_num += 1; goto next; } } } next: ; } DEBUG { printf("solved_num: %d\n", solved_num); } qsort(solutions, solved_num, sizeof(solutions[0][0])*26, cmp); /* output solutions */ for (i = 0; i < solved_num; ++i) { /*DEBUG { for (j = 0; j < 26; ++j) { printf("%c", solutions[i][j]); } puts(""); }*/ printf("%s\n", solutions[i]); } /*END ACTUAL PROCESSING*/ scanf("%d ", &n); printf("-\n"); } } int main(int args, char** argv) { get_input(); process(); return 0; }