Created
December 9, 2013 00:23
-
-
Save shz117/7865582 to your computer and use it in GitHub Desktop.
boggle game with trie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <cstdlib> | |
#include <cstring> | |
#define ENTRYNOTFOUND 0 | |
#define ENTRYFOUND 1 | |
#define NOTCOMPLETE 2 | |
struct Trie; | |
void FindWords(Trie *root); | |
struct Trie | |
{ | |
Trie(); | |
~Trie(); | |
Trie *children[26]; | |
bool isEnd; | |
}; | |
Trie::Trie() | |
{ | |
for(int i = 0; i < 26; i++) | |
children[i] = NULL; | |
isEnd = false; | |
} | |
Trie::~Trie() | |
{ | |
for(int i = 0; i < 26; i++) { | |
if(children[i]) { | |
delete children[i]; | |
children[i] = NULL; | |
} | |
} | |
} | |
void TrieInsert(Trie *root, const char *str) | |
{ | |
if(!str || *str < 'a' || *str > 'z') { | |
printf("Invalid line\n"); | |
return; | |
} | |
Trie *cur = root; | |
while(*str >= 'a' && *str <= 'z') { | |
int index = (int)(*str - 'a'); | |
if(cur->children[index] == NULL) { | |
cur->children[index] = new Trie(); | |
} | |
cur = cur->children[index]; | |
str++; | |
} | |
if(cur != root) | |
cur->isEnd = true; | |
} | |
int TrieSearch(Trie *root, const char *str) | |
{ | |
if(!str || *str < 'a' || *str > 'z') { | |
printf("Invalid string!\n"); | |
return ENTRYNOTFOUND; | |
} | |
printf("Looking for word %s\n", str); | |
Trie *cur = root; | |
while(*str >= 'a' && *str <= 'z') { | |
int index = (int)(*str - 'a'); | |
if(cur->children[index]) { | |
cur = cur->children[index]; | |
str++; | |
} | |
else | |
return ENTRYNOTFOUND; | |
} | |
if(cur != root && cur->isEnd) | |
return ENTRYFOUND; | |
else | |
return NOTCOMPLETE; | |
} | |
int main() | |
{ | |
Trie *root = new Trie(); | |
FILE *fp = fopen("dictionary.txt", "r"); | |
if(!fp) { | |
printf("Cannot find dictionary file\n"); | |
exit(-1); | |
} | |
char buff[32]; | |
int count = 0; | |
while(fgets(buff, 31, fp) != NULL) { | |
TrieInsert(root, buff); | |
memset(buff, 0, 32); | |
count++; | |
} | |
fclose(fp); | |
printf("Loaded %d words\n", count); | |
FindWords(root); | |
delete root; | |
return 0; | |
} | |
#define WIDTH 4 | |
#define HEIGHT 4 | |
bool isValidIndex(int x, int y, bool used[HEIGHT][WIDTH]) | |
{ | |
if(x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT && !used[y][x]) | |
return true; | |
else | |
return false; | |
} | |
void dfsWord(Trie *root, char *buff, int buffidx, char matrix[HEIGHT][WIDTH] | |
, bool used[HEIGHT][WIDTH], int x, int y) | |
{ | |
/* | |
if(buffidx > 0) | |
printf("Current buffer: %s\n", buff); | |
*/ | |
buff[buffidx] = matrix[y][x]; | |
used[y][x] = true; | |
int status = TrieSearch(root, buff); | |
if(status != ENTRYNOTFOUND) { | |
if(status == ENTRYFOUND) | |
printf("Found word: %s\n", buff); | |
if(isValidIndex(x-1, y, used)) | |
dfsWord(root, buff, buffidx+1, matrix, used, x-1, y); | |
if(isValidIndex(x-1, y-1, used)) | |
dfsWord(root, buff, buffidx+1, matrix, used, x-1, y-1); | |
if(isValidIndex(x, y-1, used)) | |
dfsWord(root, buff, buffidx+1, matrix, used, x, y-1); | |
if(isValidIndex(x+1, y-1, used)) | |
dfsWord(root, buff, buffidx+1, matrix, used, x+1, y-1); | |
if(isValidIndex(x+1, y, used)) | |
dfsWord(root, buff, buffidx+1, matrix, used, x+1, y); | |
if(isValidIndex(x+1, y+1, used)) | |
dfsWord(root, buff, buffidx+1, matrix, used, x+1, y+1); | |
if(isValidIndex(x, y+1, used)) | |
dfsWord(root, buff, buffidx+1, matrix, used, x, y+1); | |
if(isValidIndex(x-1, y+1, used)) | |
dfsWord(root, buff, buffidx+1, matrix, used, x-1, y+1); | |
} | |
buff[buffidx] = 0; | |
used[y][x] = false; | |
} | |
void FindWords(Trie *root) | |
{ | |
char matrix[HEIGHT][WIDTH] = { | |
{'t', 'e', 'g', 's'}, | |
{'r', 'e', 'i', 't'}, | |
{'n', 'i', 'f', 'o'}, | |
{'g', 'l', 'k', 'c'}, | |
}; | |
bool used[HEIGHT][WIDTH]; | |
for(int i = 0; i < HEIGHT; i++) { | |
for(int j = 0; j < WIDTH; j++) { | |
used[i][j] = false; | |
} | |
} | |
int tempx, tempy; | |
char buff[32]; | |
int buffidx; | |
bool noValidChoice; | |
for(int y = 0; y < HEIGHT; y++) { | |
for(int x = 0; x < WIDTH; x++) { | |
memset(buff, 0, 32); | |
buffidx = 0; | |
dfsWord(root, buff, buffidx, matrix, used, x, y); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment