用C语言编写单词搜索程序的尝试
给定一个从文件中读入的用于所有测试用例的输入字典,以及几个单词搜索网格,我想识别字典中出现在每个单词搜索网格中的所有单词 我已经阅读了dictionary.txt文件,我相信它可以读取任何字母网格,我在尝试查找dictionary.txt文件中出现的网格中的单词时遇到困难。我决定对所有方向的字符串使用递归二进制搜索,但这对我来说有点复杂 我的程序一直运行到单词搜索功能,它试图在网格中查找单词,它会崩溃,但我不知道为什么,也不确定我是否在正确的轨道上 这是我认为是错误的一部分用C语言编写单词搜索程序的尝试,c,grid,binary-search,strcmp,wordsearch,C,Grid,Binary Search,Strcmp,Wordsearch,给定一个从文件中读入的用于所有测试用例的输入字典,以及几个单词搜索网格,我想识别字典中出现在每个单词搜索网格中的所有单词 我已经阅读了dictionary.txt文件,我相信它可以读取任何字母网格,我在尝试查找dictionary.txt文件中出现的网格中的单词时遇到困难。我决定对所有方向的字符串使用递归二进制搜索,但这对我来说有点复杂 我的程序一直运行到单词搜索功能,它试图在网格中查找单词,它会崩溃,但我不知道为什么,也不确定我是否在正确的轨道上 这是我认为是错误的一部分 int binsea
int binsearch(char** dictionary, char** puzzle, int low, int high){
int mid;
if(low == 0 && high == 0){
return 0;
}
mid = (low+high)/2 ;
if(strcmp(*puzzle,dictionary[mid]) == 0){
//found a match
return 1;
}
else if(strcmp(*puzzle,dictionary[mid]) > 0){
//check upper half
return binsearch(dictionary,puzzle,mid+1,high);
}
else if(strcmp(*puzzle,dictionary[mid]) < 0){
//check lower half
return binsearch(dictionary,puzzle,low,mid-1);
}
else return 0;
}
char wordSearch(char** dictionary, char** puzzle, int row, int col){
int i, X, Y, dir = 0;
char* wordsfound[20]= {'\0'};
for (X=0;X<row+1;X++){
for(Y=0;Y<col;Y++){
for(dir=0;dir<DX_SIZE;dir++) //check every direction
for(i=0;i<19;i++){
//will continue in direction DX,DY starting at x,y
int nextX = X + DX[dir] * i;
int nextY = Y + DY[dir] * i;
if(nextX < 0 || nextX >= row) break; //keep in bounds
if(nextY < 0 || nextY >= col) break;
//store the string of letters
*wordsfound[i] = (puzzle[nextX][nextY]);
if(i>2){ //minimum word is 3
//if the string of letters is actually a word, print
int bin = binsearch(dictionary,wordsfound,1,listlength);
if(bin){
printf("%s\n",wordsfound);
}
}
}
}
}
}
int-binsearch(字符**字典,字符**拼图,低整数,高整数){
int mid;
if(低==0&&high==0){
返回0;
}
中等=(低+高)/2;
如果(strcmp(*拼图,字典[mid])==0){
//找到一根火柴
返回1;
}
else if(strcmp(*拼图,字典[mid])>0){
//检查上半部分
返回binsearch(字典、拼图、中+1、高);
}
else if(strcmp(*拼图,字典[mid])<0){
//检查下半部分
返回binsearch(字典、拼图、低、中1);
}
否则返回0;
}
字符词搜索(字符**字典、字符**拼图、整数行、整数列){
int i,X,Y,dir=0;
char*wordsfound[20]={'\0'};
对于(X=0;X此处:
因此,您将与数组元素0进行比较,并决定需要查看下半部分,现在使用low asmid-1
调用,这是-1
。哦,不!您有
char* wordsfound[20]= {'\0'};
以上仅使所有20个char指针
指向NULL
那你就是去引用了
*wordsfound[i] = (puzzle[nextX][nextY]);
因此,它崩溃是因为wordsfound[i]
为空
编辑:
因此,您必须更改wordsfound的声明。
如果您按照@John Hascall和@J V A的建议将其更改为char words found[20],
然后,您需要执行以下操作:
char wordSearch(char** dictionary, char** puzzle, int row, int col){
int i, X, Y, dir = 0;
char wordsfound[20]= {'\0'};
for (X=0;X<row+1;X++){
for(Y=0;Y<col;Y++){
for(dir=0;dir<DX_SIZE;dir++) //check every direction
for(i=0;i<19;i++){
//will continue in direction DX,DY starting at x,y
int nextX = X + DX[dir] * i;
int nextY = Y + DY[dir] * i;
if(nextX < 0 || nextX >= row) break; //keep in bounds
if(nextY < 0 || nextY >= col) break;
//store the string of letters
wordsfound[i] = (puzzle[nextX][nextY]);
if(i>2){ //minimum word is 3
wordsfound[i+1]= '\0'; // yes you need null char before passing to binsearch for strcmp to work.
//if the string of letters is actually a word, print
int bin = binsearch(dictionary, &wordsfound,1,listlength);
if(bin){
printf("%s\n",wordsfound);
}
}
}
}
}
}
char-wordSearch(char**dictionary,char**puzzle,int-row,int-col){
int i,X,Y,dir=0;
char-wordsfound[20]={'\0'};
对于(X=0;Xif(low==0&&high==0){return 0;}
终止条件不好。我认为他们不需要20个字符的指针。我认为他们只需要char-wordsfound[20]={'\0'};
和wordsfound[I]=(puzzle[nextX][nextY]);
(和wordsfound[I+1]='\0';
)是的,您已经识别了它。但是您建议的修复(您需要分配内存或只使用二维数组。
)没有包含正确的修复(删除*
).所以我把它改为只传递wordsfound,去掉*第二个参数拼图,形成strcmp中的一个,接受了你的建议,但由于某种原因它仍然崩溃。如果我的目标是一次只存储一个字符串,wordsfound数组的格式也不是实现它的方法。就像它在其中存储了3个字符一样,我是否需要检查字典中是否存在3个字符的字符串,并在添加每个字符后进行检查时一直添加字符,直到添加19个字符。现在它在哪里崩溃?在尝试该操作后仍然崩溃:/maybe something this is crash it?崩溃在哪一行?在我通过标准输入输入输入网格数组后。因此我可以输入测试用例的数量,然后输入行和列的数字,然后逐行输入一个字符数组,然后它将打印“Words found Grid#1:并崩溃”
mid = (low+high)/2 ; /* this is zero */
char* wordsfound[20]= {'\0'};
*wordsfound[i] = (puzzle[nextX][nextY]);
char wordSearch(char** dictionary, char** puzzle, int row, int col){
int i, X, Y, dir = 0;
char wordsfound[20]= {'\0'};
for (X=0;X<row+1;X++){
for(Y=0;Y<col;Y++){
for(dir=0;dir<DX_SIZE;dir++) //check every direction
for(i=0;i<19;i++){
//will continue in direction DX,DY starting at x,y
int nextX = X + DX[dir] * i;
int nextY = Y + DY[dir] * i;
if(nextX < 0 || nextX >= row) break; //keep in bounds
if(nextY < 0 || nextY >= col) break;
//store the string of letters
wordsfound[i] = (puzzle[nextX][nextY]);
if(i>2){ //minimum word is 3
wordsfound[i+1]= '\0'; // yes you need null char before passing to binsearch for strcmp to work.
//if the string of letters is actually a word, print
int bin = binsearch(dictionary, &wordsfound,1,listlength);
if(bin){
printf("%s\n",wordsfound);
}
}
}
}
}
}