#define N 4
void FindWordsBoggle(char board[][N])
{
int i=0,j=0;
char* outStr = (char*) malloc((N*N+1)*sizeof(char));
int* occupMap = (int*) calloc((N*N),sizeof(int));
int level = 0;
RecursiveBoggle(board, outStr, level, occupMap);
free(occupMap);
free(outStr);
}
void RecursiveBoggle(char board[][N], char* outStr, int level, int*
occupMap)
{
int* occupMapCopy = (int*) malloc((N*N)*sizeof(int));
memcpy(occupMapCopy, occupMap, N*N*sizeof(int));
int i=0,j=0;
for ( ; i < N; i++)
{
for (; j < N; j++)
{
if (!occupMapCopy[i*N+j])
{
outStr[level] = board[i][j];
outStr[level+1] = '\0';
/*Find whether the current string is available as a word.*/
if (FindInDictionary(outStr))
printf("%s\n",outStr);
if (FindPrefixInDictionary(outStr))
{
occupMapCopy[i*N+j] = 2;
/*Label neighbors as traversable*/
LabelNeighbors(occupMapCopy, i, j);
RecursiveBoggle(board, outStr, level+1, occupMapCopy);
}
}
}
}
free(occupMapCopy);
}
void LabelNeighbors(int* occupMap, int i, int j)
{
int ii=0,jj=0;
for (; ii < N; ++ii)
{
for (; jj < N; ++jj)
{
if (occupMap[ii*N+jj] == 2)
/*already used*/
continue;
else if ( abs(ii-i) <=1 && abs(jj-j) <=1 )
/*can be next character*/
occupMap[ii*N+jj] = 0;
else occupMap[ii*N+jj] = 1; /*not reachable*/
}
}
}