我有下面的代码可以解决一个难题。它允许输入dictionary.txt 文件,将文件中的每一行放入链表中,然后允许输入用户输入的boggle 游戏并尝试解决它。我已经达到了解决它的地步,但现在我有点卡住了。在我的求解函数中,我使用递归调用来尝试来自用户输入的所有可能匹配项,通过一次取一个字母来查看它是否是一个单词,创建一个单词并将其与 dictionary.txt 进行比较以查看它是否真的一个字。
我遇到的问题是跟踪我访问过的信件。基本上,我递归地调用我制作的二维数组,将所有位置设置为 0,并在我访问该位置时将其更改为 1。tl;dr 如何使用递归调用从二维数组更改变量。
注意:我这里有很多 printf 语句仅用于调试目的。
我坚持的代码位:
void wordChecker(struct trie* myDictionary, char boggleBoard[][4], char isThisAWord[], int i, int j, int x, int y, int tracker[][4]){
printf("%d\n", tracker[i][j]);
if (searchWord(myDictionary, isThisAWord, 0) == 1){
printf("found a word in word checker %s", isThisAWord);
return;
}
if (searchWord(myDictionary, isThisAWord, 0) == 0 && inBounds((i+DX[x]),(j+DY[y]))
&& haveWeBeenHere(i+DX[x],j+DY[y], tracker)){
strncat(isThisAWord, &boggleBoard[i+DX[x]][j+DY[y]], 1);
printf("the board is on %c\n", boggleBoard[i+DX[x]][j+DY[y]]);
printf("the current word %s\n", isThisAWord);
///I'm 100% sure its here that lies the problem, partly because I don't know what I'm doing.
///the tracker array isn't changing here and I'm sure its because I'm doing it wrong.
i = i+DX[x];
j = j+DY[y];
tracker[i+DX[x]][j+DY[y]] = 1;
x = -1;
y = -1;
wordChecker(myDictionary, boggleBoard, isThisAWord, i, j, x+1, y+1, tracker);
} else if (x > 8)
return;
wordChecker(myDictionary, boggleBoard, isThisAWord, i, j, x+1, y+1, tracker);
return;
}
int haveWeBeenHere(int i, int j, int tracker[][4]){
if(tracker[i][j] == 0){
tracker[i][j] == 1;
return 1;
}
return 0;
}
整个代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct trie{
int isWord;
struct trie* nextLetter[26];
} trie;
const int DX_SIZE = 8;
const int DX[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int DY[] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct trie* initializer();
struct trie* insertWord(struct trie* myTrie, char word[], int num);
void freeDictionary(struct trie* myTrie);
int searchWord(struct trie* myTrie, char word[], int num);
int inBounds(int x, int y);
void wordChecker(struct trie* myDictionary, char boggleBoard[][4], char isThisAWord[], int i, int j, int x, int y, int tracker[][4]);
int main (){
struct trie* myDictionary = initializer();
int dictionarySize, i, j, boggleGames;
char boggleBoard[4][4];
FILE *fp;
fp = fopen("sampledictionary.txt", "r");
fscanf(fp, "%d", &dictionarySize);
for(i=0; i<dictionarySize; i++){
char word[100];
fscanf(fp, "%s", word);
insertWord(myDictionary, word, 0);
}
scanf("%d", &boggleGames);
for(i=0; i<4; i++)
scanf("%s", boggleBoard[i]);
char isThisAWord[16] = {0};
for (i=0; i<4; i++){
for(j=0; j<4; j++){
int tracker[4][4] = {0};
tracker[i][j] = 1;
strncat(isThisAWord, &boggleBoard[i][j], 1);
printf("copying %c\n", isThisAWord[0]);
wordChecker(myDictionary, boggleBoard, isThisAWord, i, j, 0, 0, tracker);
}
}
/* debugger for dictionary.txt
printf("search for? ");
char word[100];
scanf("%s", word);
if (searchWord(myDictionary, word, 0) == 0){
printf("%s is part of a word", word);
}
else if (searchWord(myDictionary, word, 0) == 1){
printf("%s is a word in main");
}
else if (searchWord(myDictionary, word, 0) == 2){
printf("%s is not a word", word);
}
*/
freeDictionary(myDictionary);
}
void wordChecker(struct trie* myDictionary, char boggleBoard[][4], char isThisAWord[], int i, int j, int x, int y, int tracker[][4]){
printf("%d\n", tracker[i][j]);
if (searchWord(myDictionary, isThisAWord, 0) == 1){
printf("found a word in word checker %s", isThisAWord);
return;
}
if (searchWord(myDictionary, isThisAWord, 0) == 0 && inBounds((i+DX[x]),(j+DY[y]))
&& haveWeBeenHere(i+DX[x],j+DY[y], tracker)){
strncat(isThisAWord, &boggleBoard[i+DX[x]][j+DY[y]], 1);
printf("the board is on %c\n", boggleBoard[i+DX[x]][j+DY[y]]);
printf("the current word %s\n", isThisAWord);
i = i+DX[x];
j = j+DY[y];
tracker[i+DX[x]][j+DY[y]] = 1;
x = -1;
y = -1;
wordChecker(myDictionary, boggleBoard, isThisAWord, i, j, x+1, y+1, tracker);
} else if (x > 8)
return;
wordChecker(myDictionary, boggleBoard, isThisAWord, i, j, x+1, y+1, tracker);
return;
}
int inBounds(int x, int y){
if (x > 4 || x < 0 || y > 4 || y < 0)
return 0;
return 1;
}
int haveWeBeenHere(int i, int j, int tracker[][4]){
if(tracker[i][j] == 0){
tracker[i][j] == 1;
return 1;
}
return 0;
}
struct trie* initializer(){
struct trie* myTrie = malloc(sizeof(struct trie));
myTrie-> isWord = 0;
int i;
for(i=0; i<26; i++)
myTrie->nextLetter[i] = NULL;
return myTrie;
}
struct trie* insertWord(struct trie* myTrie, char word[], int num){
if (num == strlen(word)){
myTrie->isWord = 1;
return;
}
int nextIndex = word[num] - 'a';
if(myTrie->nextLetter[nextIndex] == NULL)
myTrie->nextLetter[nextIndex] = initializer();
insertWord(myTrie->nextLetter[nextIndex], word, num+1);
}
int searchWord(struct trie* myTrie, char word[], int num){
int test = strlen(word);
int nextIndex = word[num] - 'a';
if (num == strlen(word))
return myTrie->isWord;
if (myTrie->nextLetter[nextIndex] == NULL)
return 2;
return searchWord(myTrie->nextLetter[nextIndex], word, num+1);
}
void freeDictionary(struct trie* myTrie){
int i;
for(i=0; i<26; i++)
if (myTrie->nextLetter[i] != NULL)
freeDictionary(myTrie->nextLetter[i]);
free(myTrie);
}